Skip to content

Commit

Permalink
Add doube quotes to escape special chars during interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
c-py committed Dec 15, 2020
1 parent fe4f1cc commit a6380f4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
9 changes: 7 additions & 2 deletions dotenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ export_envs() {
continue
fi

value=$(eval echo "$temp")
# Strip any existing quotes
temp="${temp%[\'\"]}";
temp="${temp#[\'\"]}";
# Add new double quotes for interpolation
value=$(eval echo "\"$temp\"");

eval export "$key='$value'";
echo "$key=$value" >> $GITHUB_ENV
echo "$key=$value" >> $GITHUB_ENV;
done < $1
}

Expand Down
4 changes: 3 additions & 1 deletion tests/.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ TEST_INTERPOLATION="$TEST_UNQUOTED d=4"
TEST_EXISTING="new-value"
# Test secrets override defaults
TEST_DOTENV_OVERRIDES_DEFAULT="expected"
# Test special characters are escaped
TEST_SPECIAL_CHARACTER=special(character
# Test that the last variable is parsed despite no trailing new line
TEST_NO_NEWLINE="still there"
TEST_NO_NEWLINE="still there"
1 change: 1 addition & 0 deletions tests/dotenv-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ main() {
assert_equal "$TEST_NO_NEWLINE" 'still there' 'Testing parsing of last line'
assert_equal "$TEST_DEFAULT_ENVFILE" 'expected' 'Test loading variables from default.env file'
assert_equal "$TEST_DOTENV_OVERRIDES_DEFAULT" 'expected' 'Test .env variables override variables from default.env file'
assert_equal "$TEST_SPECIAL_CHARACTER" 'special(character' 'Testing special characters'

TEST_NO_ENVFILE=`DOTENV_FILE=nonexistent.env ../dotenv.sh 2>&1` # Close stdout for this test
assert_equal "$TEST_NO_ENVFILE" "nonexistent.env file not found" 'Test error message from missing .env file'
Expand Down

0 comments on commit a6380f4

Please sign in to comment.