Skip to content

Commit

Permalink
Merge pull request #2 from mammo0/fix_for_#1
Browse files Browse the repository at this point in the history
  • Loading branch information
c-py authored Nov 2, 2020
2 parents d5fedff + c9f0e93 commit 3e796a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
47 changes: 25 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ Adapted from https://github.com/madcoda/dotenv-shell

## Outputs

[set-env](https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable) workflow commands to stdout.
[`echo "{name}={value}"`](https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable) workflow commands to `$GITHUB_ENV`.

```
::set-env name=TEST_DEFAULT_ENVFILE::expected
::set-env name=TEST_DOTENV_OVERRIDES_DEFAULT::unexpected
::set-env name=TEST_UNQUOTED::unexpected
::set-env name=TEST_UNQUOTED::a=1 b=2 c=3
::set-env name=TEST_SINGLE_QUOTED::1 2 3 4
::set-env name=TEST_DOUBLE_QUOTED::1 2 3 4
::set-env name=TEST_INTERPOLATION::a=1 b=2 c=3 d=4
::set-env name=TEST_EXISTING::new-value
::set-env name=TEST_DOTENV_OVERRIDES_DEFAULT::expected
::set-env name=TEST_NO_NEWLINE::still there
```shell
echo "TEST_DEFAULT_ENVFILE=expected" >> $GITHUB_ENV
echo "TEST_DOTENV_OVERRIDES_DEFAULT=unexpected" >> $GITHUB_ENV
echo "TEST_UNQUOTED=unexpected" >> $GITHUB_ENV
echo "TEST_UNQUOTED=a=1 b=2 c=3" >> $GITHUB_ENV
echo "TEST_SINGLE_QUOTED=1 2 3 4" >> $GITHUB_ENV
echo "TEST_DOUBLE_QUOTED=1 2 3 4" >> $GITHUB_ENV
echo "TEST_INTERPOLATION=a=1 b=2 c=3 d=4" >> $GITHUB_ENV
echo "TEST_EXISTING=new-value" >> $GITHUB_ENV
echo "TEST_DOTENV_OVERRIDES_DEFAULT=expected" >> $GITHUB_ENV
echo "TEST_NO_NEWLINE=still there" >> $GITHUB_ENV
```

## Example Usage
Expand All @@ -39,16 +39,18 @@ with:

```
$ bash tests/dotenv-test.sh
::set-env name=TEST_DEFAULT_ENVFILE::expected
::set-env name=TEST_DOTENV_OVERRIDES_DEFAULT::unexpected
::set-env name=TEST_UNQUOTED::unexpected
::set-env name=TEST_UNQUOTED::a=1 b=2 c=3
::set-env name=TEST_SINGLE_QUOTED::1 2 3 4
::set-env name=TEST_DOUBLE_QUOTED::1 2 3 4
::set-env name=TEST_INTERPOLATION::a=1 b=2 c=3 d=4
::set-env name=TEST_EXISTING::new-value
::set-env name=TEST_DOTENV_OVERRIDES_DEFAULT::expected
::set-env name=TEST_NO_NEWLINE::still there
Contents of $GITHUB_ENV file:
TEST_DEFAULT_ENVFILE=expected
TEST_DOTENV_OVERRIDES_DEFAULT=unexpected
TEST_UNQUOTED=unexpected
TEST_UNQUOTED=a=1 b=2 c=3
TEST_SINGLE_QUOTED=1 2 3 4
TEST_DOUBLE_QUOTED=1 2 3 4
TEST_INTERPOLATION=a=1 b=2 c=3 d=4
TEST_EXISTING=new-value
TEST_DOTENV_OVERRIDES_DEFAULT=expected
TEST_NO_NEWLINE=still there
Testing blank line parsing: ok
Testing unquoted: ok
Testing single quoted: ok
Expand All @@ -58,5 +60,6 @@ Testing overwrite of existing variables: ok
Testing parsing of last line: ok
Test loading variables from default.env file: ok
Test .env variables override variables from default.env file: ok
Test error message from missing .env file: ok
$
```
6 changes: 3 additions & 3 deletions dotenv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export_envs() {
continue
fi

value=$(eval echo "$temp")
eval export "$key='$value'";
echo "::set-env name=$key::$value"
value=$(eval echo "$temp")
eval export "$key='$value'";
echo "$key=$value" >> $GITHUB_ENV
done < $1
}

Expand Down
9 changes: 8 additions & 1 deletion tests/dotenv-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
main() {
cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1

export GITHUB_ENV=$(mktemp)
# shellcheck disable=SC1091
export TEST_EXISTING="expected"
export DOTENV_DEFAULT="default.env"
source ../dotenv.sh

echo "Contents of \$GITHUB_ENV file:"
cat "$GITHUB_ENV"
echo

echo "Testing blank line parsing: ok" # i.e. didn't crash
assert_equal "$TEST_UNQUOTED" 'a=1 b=2 c=3' 'Testing unquoted'
assert_equal "$TEST_SINGLE_QUOTED" '1 2 3 4' 'Testing single quoted'
Expand All @@ -18,8 +23,10 @@ main() {
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'

TEST_NO_ENVFILE=`DOTENV_FILE=nonexistent.env ../dotenv.sh 2>&1 >&3 3>&-` # Close stdout for this test
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'

rm "$GITHUB_ENV"
}

assert_equal() {
Expand Down

0 comments on commit 3e796a9

Please sign in to comment.