From 6ad89ca490eeeb1e50f14f73a664a8311fa6723c Mon Sep 17 00:00:00 2001 From: Dmitri Akatov Date: Thu, 15 Aug 2013 12:04:08 -0700 Subject: [PATCH] Add option to ignore missing source files This is useful when using dynamic fresh lines: fresh zsh/$PROFILE --file=~/.zshrc --ignore-missing Fixes #88. --- bin/fresh | 10 ++++++-- test/fresh_test.sh | 64 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/bin/fresh b/bin/fresh index 9f9bf9e..e16895e 100755 --- a/bin/fresh +++ b/bin/fresh @@ -162,7 +162,7 @@ EOF if [[ $(find "$SOURCE_DIR" -path "$SOURCE_DIR/$FILE_NAME" | wc -l) -lt 1 ]]; then # but not if we are locking to a git ref # since we would be checking the working tree - if [[ ! -n "$REF" ]] && [[ "$FILE_NAME" != '.' ]]; then + if [[ ! -n "$REF" ]] && [[ "$FILE_NAME" != '.' ]] && [[ -z "$IGNORE_MISSING" ]]; then _fatal_error "Could not find \"$FILE_NAME\" source file." fi fi @@ -200,7 +200,7 @@ _file_list() { fi done < <(git ls-tree -r --name-only "$REF") cd "$OLDPWD" - if [[ "$MATCHED" == 0 ]]; then + if [[ "$MATCHED" == 0 ]] && [[ -z "$IGNORE_MISSING" ]]; then _fatal_error "Could not find \"$FILE_NAME\" source file." fi else @@ -440,6 +440,7 @@ _dsl_fresh_options() { DEFAULT_REF= DEFAULT_MARKER= DEFAULT_FILTER= + DEFAULT_IGNORE_MISSING= if [[ $# -gt 0 ]]; then # __FRESH_OPTIONS__ placeholder as _parse_fresh_dsl_args expects a filename @@ -450,6 +451,7 @@ _dsl_fresh_options() { DEFAULT_REF="$REF" DEFAULT_MARKER="$MARKER" DEFAULT_FILTER="$FILTER" + DEFAULT_IGNORE_MISSING="$IGNORE_MISSING" fi } @@ -466,6 +468,7 @@ _parse_fresh_dsl_args() { REF="$DEFAULT_REF" MARKER="$DEFAULT_MARKER" FILTER="$DEFAULT_FILTER" + IGNORE_MISSING="$DEFAULT_IGNORE_MISSING" while [ $# -gt 0 ] do @@ -502,6 +505,9 @@ _parse_fresh_dsl_args() { --filter) _fatal_error "You must specify a filter program." ;; + --ignore-missing) + IGNORE_MISSING="1" + ;; -*) _fatal_error "Unknown option: $1" ;; diff --git a/test/fresh_test.sh b/test/fresh_test.sh index 6b006b9..f976c86 100755 --- a/test/fresh_test.sh +++ b/test/fresh_test.sh @@ -1,6 +1,6 @@ #!/bin/bash -it_builds_local_shell_files() { +_build_local_shell_files() { echo fresh aliases/git >> $FRESH_RCFILE echo fresh aliases/ruby >> $FRESH_RCFILE @@ -8,9 +8,9 @@ it_builds_local_shell_files() { echo "alias gs='git status'" >> $FRESH_LOCAL/aliases/git echo "alias gl='git log'" >> $FRESH_LOCAL/aliases/git echo "alias rake='bundle exec rake'" >> $FRESH_LOCAL/aliases/ruby +} - runFresh - +_assert_local_shell_files() { assertFileMatches $FRESH_PATH/build/shell.sh <> $FRESH_RCFILE @@ -90,6 +96,13 @@ it_errors_with_missing_local_file() { runFresh fails } +it_builds_local_shell_files_with_ignore_missing() { + echo fresh aliases/haskell --ignore-missing >> $FRESH_RCFILE + _build_local_shell_files + runFresh + _assert_local_shell_files +} + it_preserves_existing_compiled_file_when_failing() { mkdir -p $FRESH_PATH/build echo 'existing file' > $FRESH_PATH/build/shell.sh @@ -294,6 +307,39 @@ git ls-tree -r --name-only abc1237 EOF } +it_does_not_error_if_source_file_missing_at_ref_with_ignore_missing() { + echo fresh repo/name bad-file --ref=abc1237 --ignore-missing >> $FRESH_RCFILE + mkdir -p $FRESH_PATH/source/repo/name + stubGit + + runFresh + + assertFileMatches $SANDBOX_PATH/git.log <> $FRESH_RCFILE + echo 'fresh repo/name missing --file --ref=abc1237 --ignore-missing' >> $FRESH_RCFILE + mkdir -p $FRESH_PATH/source/repo/name + stubGit + + runFresh + + assertFileMatches $SANDBOX_PATH/git.log <> $FRESH_RCFILE @@ -438,6 +484,18 @@ EOF assertFalse 'not writable' '[ -w $FRESH_PATH/build/vimrc ]' } +it_does_not_create_a_file_when_single_source_is_missing_with_ignore_missing() { + echo 'fresh tmux.conf --file --ignore-missing' >> $FRESH_RCFILE + echo 'fresh ghci --file --ignore-missing' >> $FRESH_RCFILE + mkdir -p $FRESH_LOCAL + touch $FRESH_LOCAL/tmux.conf + + runFresh + + assertTrue 'exists' '[ -e $FRESH_PATH/build/tmux.conf ]' + assertFalse 'does not exist' '[ -e $FRESH_PATH/build/ghci ]' +} + it_builds_generic_files_with_globbing() { echo "fresh 'file*' --file" >> $FRESH_RCFILE