fix: append trailing newline to .tool-versions files when missing (#1310)

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
This commit is contained in:
Trevor Brown 2022-07-25 09:23:43 -04:00 committed by GitHub
parent a75f851f7a
commit eb7dac3a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -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
}

View File

@ -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" ]
@ -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" ]