Skip to content

Commit

Permalink
Add option to ignore missing source files
Browse files Browse the repository at this point in the history
This is useful when using dynamic fresh lines:

    fresh zsh/$PROFILE --file=~/.zshrc --ignore-missing

Fixes #88.
  • Loading branch information
akatov authored and twe4ked committed Aug 19, 2013
1 parent 8d74a92 commit 6ad89ca
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
10 changes: 8 additions & 2 deletions bin/fresh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -450,6 +451,7 @@ _dsl_fresh_options() {
DEFAULT_REF="$REF"
DEFAULT_MARKER="$MARKER"
DEFAULT_FILTER="$FILTER"
DEFAULT_IGNORE_MISSING="$IGNORE_MISSING"
fi
}

Expand All @@ -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
Expand Down Expand Up @@ -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"
;;
Expand Down
64 changes: 61 additions & 3 deletions test/fresh_test.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/bin/bash

it_builds_local_shell_files() {
_build_local_shell_files() {
echo fresh aliases/git >> $FRESH_RCFILE
echo fresh aliases/ruby >> $FRESH_RCFILE

mkdir -p $FRESH_LOCAL/aliases
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 <<EOF
export PATH="\$HOME/bin:\$PATH"
export FRESH_PATH="$FRESH_PATH"
Expand All @@ -29,6 +29,12 @@ EOF
assertFalse 'not writable' '[ -w $FRESH_PATH/build/shell.sh ]'
}

it_builds_local_shell_files() {
_build_local_shell_files
runFresh
_assert_local_shell_files
}

it_builds_local_shell_files_with_spaces() {
echo "fresh 'aliases/foo bar'" >> $FRESH_RCFILE

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <<EOF
cd $FRESH_PATH/source/repo/name
git ls-tree -r --name-only abc1237
EOF
}

it_builds_files_with_ref_and_ignore_missing() {
echo 'fresh repo/name ackrc --file --ref=abc1237 --ignore-missing' >> $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 <<EOF
cd $FRESH_PATH/source/repo/name
git ls-tree -r --name-only abc1237
cd $FRESH_PATH/source/repo/name
git show abc1237:ackrc
cd $FRESH_PATH/source/repo/name
git ls-tree -r --name-only abc1237
EOF
assertTrue 'exists' '[ -e $FRESH_PATH/build/ackrc ]'
assertFalse 'does not exist' '[ -e $FRESH_PATH/missing ]'
}

it_ignores_subdirectories_when_globbing_from_working_tree() {
echo "fresh 'recursive-test/*'" >> $FRESH_RCFILE

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 6ad89ca

Please sign in to comment.