Skip to content

Commit

Permalink
fix: append trailing newline to .tool-versions files when missing (#1310
Browse files Browse the repository at this point in the history
)

If a .tool-versions file did not end with a newline new tools and
versions would get appended to the same line rather than properly
added on a new line in the file

Fixes #1299
  • Loading branch information
Stratus3D authored Jul 25, 2022
1 parent a75f851 commit eb7dac3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ version_command() {
sed -i.bak -e "s|^$plugin_name .*$|$plugin_name ${resolved_versions[*]}|" "$file"
rm -f "$file".bak
else
# Add a trailing newline at the end of the file if missing
[[ -n "$(tail -c1 "$file")" && -f "$file" ]] && printf '\n' >>"$file"

# Add a new version line to the end of the file
printf "%s %s\\n" "$plugin_name" "${resolved_versions[*]}" >>"$file"
fi
}
Expand Down
32 changes: 32 additions & 0 deletions test/version_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ teardown() {
[ "$(cat $PROJECT_DIR/.tool-versions)" = "dummy 1.1.0" ]
}

@test "local should append trailing newline before appending new version when missing" {
echo -n 'foobar 1.0.0' >>$PROJECT_DIR/.tool-versions

run asdf local "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = $'foobar 1.0.0\ndummy 1.1.0' ]
}

@test "local should not append trailing newline before appending new version when one present" {
echo 'foobar 1.0.0' >>$PROJECT_DIR/.tool-versions

run asdf local "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $PROJECT_DIR/.tool-versions)" = $'foobar 1.0.0\ndummy 1.1.0' ]
}

@test "local should fail to set a path:dir if dir does not exists " {
run asdf local "dummy" "path:$PROJECT_DIR/local"
[ "$output" = "version path:$PROJECT_DIR/local is not installed for dummy" ]
Expand Down Expand Up @@ -236,6 +252,22 @@ teardown() {
[ "$(cat $HOME/.tool-versions)" = "dummy 1.1.0" ]
}

@test "global should append trailing newline before appending new version when missing" {
echo -n 'foobar 1.0.0' >>$HOME/.tool-versions

run asdf global "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = $'foobar 1.0.0\ndummy 1.1.0' ]
}

@test "global should not append trailing newline before appending new version when one present" {
echo 'foobar 1.0.0' >>$HOME/.tool-versions

run asdf global "dummy" "1.1.0"
[ "$status" -eq 0 ]
[ "$(cat $HOME/.tool-versions)" = $'foobar 1.0.0\ndummy 1.1.0' ]
}

@test "global should fail to set a path:dir if dir does not exists " {
run asdf global "dummy" "path:$PROJECT_DIR/local"
[ "$output" = "version path:$PROJECT_DIR/local is not installed for dummy" ]
Expand Down

0 comments on commit eb7dac3

Please sign in to comment.