From b47d3837d452ca6d2509d2524c7a08c701e84367 Mon Sep 17 00:00:00 2001 From: Nat Pryce Date: Wed, 25 Jul 2018 19:27:20 +0100 Subject: [PATCH] Implement issue #62: search for ADR dir in parents of current directory. --- Makefile | 17 +++++++++---- approve | 2 +- src/_adr_dir | 32 +++++++++++++++++++----- tests/search-for-adr-dir.expected | 13 ++++++++++ tests/search-for-adr-dir.sh | 7 ++++++ tests/search-for-custom-adr-dir.expected | 17 +++++++++++++ tests/search-for-custom-adr-dir.sh | 8 ++++++ 7 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 tests/search-for-adr-dir.expected create mode 100644 tests/search-for-adr-dir.sh create mode 100644 tests/search-for-custom-adr-dir.expected create mode 100644 tests/search-for-custom-adr-dir.sh diff --git a/Makefile b/Makefile index 9d92147..736a0db 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,17 @@ TESTS:=$(wildcard tests/*.sh) SRC:=$(wildcard src/*) -check: $(TESTS:tests/%.sh=build/tests/%.diff) +# Run tests outside the project directory so that they cannot interfere with the project's +# own ADR directory +BUILDDIR:=/tmp/adr-tools-build + +check: $(TESTS:tests/%.sh=$(BUILDDIR)/tests/%.diff) @echo SUCCESS -build/tests/%.diff: build/tests/%.output tests/%.expected +$(BUILDDIR)/tests/%.diff: $(BUILDDIR)/tests/%.output tests/%.expected @diff --side-by-side $^ > $@ || ! cat $@ -build/tests/%.output: tests/%.sh tests/%.expected $(SRC) +$(BUILDDIR)/tests/%.output: tests/%.sh tests/%.expected $(SRC) @echo TEST: $* @rm -rf $(dir $@)/$* @mkdir -p $(dir $@)/$* @@ -21,8 +25,11 @@ build/tests/%.output: tests/%.sh tests/%.expected $(SRC) /bin/sh -v $(abspath $<) > $(abspath $@) 2>&1) || ! cat $@ clean: - rm -rf build/ + rm -rf /tmp/adr-tools-build + +show-%: + @echo "$* ($(flavor $*)) = $($*)" .PHONY: all clean -.PRECIOUS: build/tests/%.output +.PRECIOUS: $(BUILDDIR)/tests/%.output .DELETE_ON_ERROR: diff --git a/approve b/approve index 1a41307..e880864 100755 --- a/approve +++ b/approve @@ -5,4 +5,4 @@ test="${1:?test}" test_name=$(basename $test | sed 's/\..*//') -cp -v build/tests/$test_name.output tests/$test_name.expected +cp -v /tmp/adr-tools-build/tests/$test_name.output tests/$test_name.expected diff --git a/src/_adr_dir b/src/_adr_dir index a3555c3..27a3c48 100755 --- a/src/_adr_dir +++ b/src/_adr_dir @@ -2,9 +2,29 @@ set -e eval "$($(dirname $0)/adr-config)" -if [ -f .adr-dir ] -then - cat .adr-dir -else - echo doc/adr -fi +reldir=. + +function mkrel() { + local d=$reldir/$1 + echo ${d#./} +} + +function absdir() { + (cd $(dirname $1) && pwd -P) +} + +while [ $(absdir $reldir) != / ] +do + if [ -f $(mkrel .adr-dir) ] + then + mkrel $(cat $(mkrel .adr-dir)) + exit + elif [ -d $(mkrel doc/adr) ] + then + mkrel doc/adr + exit + else + reldir=$reldir/.. + fi +done +echo doc/adr diff --git a/tests/search-for-adr-dir.expected b/tests/search-for-adr-dir.expected new file mode 100644 index 0000000..d52b22c --- /dev/null +++ b/tests/search-for-adr-dir.expected @@ -0,0 +1,13 @@ +adr new First Record +doc/adr/0001-first-record.md +mkdir subdir +cd subdir +adr new Second Record +../doc/adr/0002-second-record.md +adr list +../doc/adr/0001-first-record.md +../doc/adr/0002-second-record.md +cd .. +adr list +doc/adr/0001-first-record.md +doc/adr/0002-second-record.md diff --git a/tests/search-for-adr-dir.sh b/tests/search-for-adr-dir.sh new file mode 100644 index 0000000..9dbcbd4 --- /dev/null +++ b/tests/search-for-adr-dir.sh @@ -0,0 +1,7 @@ +adr new First Record +mkdir subdir +cd subdir +adr new Second Record +adr list +cd .. +adr list diff --git a/tests/search-for-custom-adr-dir.expected b/tests/search-for-custom-adr-dir.expected new file mode 100644 index 0000000..a9b41f6 --- /dev/null +++ b/tests/search-for-custom-adr-dir.expected @@ -0,0 +1,17 @@ +adr init architecture-log +architecture-log/0001-record-architecture-decisions.md +adr new First Record +architecture-log/0002-first-record.md +mkdir subdir +cd subdir +adr new Second Record +../architecture-log/0003-second-record.md +adr list +../architecture-log/0001-record-architecture-decisions.md +../architecture-log/0002-first-record.md +../architecture-log/0003-second-record.md +cd .. +adr list +architecture-log/0001-record-architecture-decisions.md +architecture-log/0002-first-record.md +architecture-log/0003-second-record.md diff --git a/tests/search-for-custom-adr-dir.sh b/tests/search-for-custom-adr-dir.sh new file mode 100644 index 0000000..c80060e --- /dev/null +++ b/tests/search-for-custom-adr-dir.sh @@ -0,0 +1,8 @@ +adr init architecture-log +adr new First Record +mkdir subdir +cd subdir +adr new Second Record +adr list +cd .. +adr list