Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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