-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Arkoniak/dotenvx
multiple refactorings
- Loading branch information
Showing
11 changed files
with
334 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "ConfigEnv" | ||
uuid = "01234589-554d-41b7-ae5c-7b6ee2db6796" | ||
authors = ["Andrey Oskin <[email protected]>"] | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
|
||
[deps] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1 @@ | ||
TEST=OK | ||
#COMMENT=YES | ||
BAD= asd asd asd asd \n asd asd something | ||
|
||
OTHERVAR=asd | ||
EMPTY= | ||
USER=john | ||
|
||
CONCOMILLAS="something with spac\nes" | ||
CONCOMIL='something with spac\nes' | ||
JSON={"key":"value"} | ||
|
||
CON_ESPACIOS1= something | ||
CON_ESPACIOS2=" something " | ||
QWERTY = ZXC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FOO = BAR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
QWE = ASD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FOO = FUU |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
TEST=OK | ||
#COMMENT=YES | ||
BAD= asd asd asd asd \n asd asd something | ||
|
||
OTHERVAR=asd | ||
EMPTY= | ||
USER=foo | ||
|
||
DOUBLEQUOTES="something with spac\nes" | ||
SINGLEQOUTES='something with spac\nes' | ||
JSON={"key":"value"} | ||
|
||
MANY_SPACES= something | ||
QUOTED_SPACES=" something " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,28 @@ | ||
module TestDotEnv | ||
|
||
using ConfigEnv | ||
|
||
using Test | ||
|
||
const dir = dirname(@__FILE__) | ||
|
||
# There is no "USER" variable on windows. | ||
initial_value = haskey(ENV, "USER") ? ENV["USER"] : "WINDOWS" | ||
ENV["USER"] = initial_value | ||
|
||
@testset "basic" begin | ||
# basic input | ||
str = "BASIC=basic" | ||
file = joinpath(dir, ".env.override") | ||
file2 = joinpath(dir, ".env") | ||
|
||
# can turn off override of ENV vars | ||
cfg = dotenv(file, false) | ||
@test ENV["USER"] != cfg["USER"] | ||
@test ENV["USER"] == initial_value | ||
|
||
# iobuffer, string, file | ||
@test ConfigEnv.parse(str) == Dict("BASIC"=>"basic") | ||
@test ConfigEnv.parse(read(file)) == Dict("CUSTOMVAL123"=>"yes","USER"=>"replaced value") | ||
@test dotenv(file).dict == Dict("CUSTOMVAL123"=>"yes","USER"=>"replaced value") | ||
|
||
@test isempty(dotenv("inexistentfile.env")) | ||
|
||
# length of returned values | ||
@test length(dotenv(file2).dict) == 10 | ||
|
||
# appropiately loaded into ENV if CUSTOM_VAL is non existent | ||
@test ENV["CUSTOMVAL123"] == "yes" | ||
|
||
# Test that EnvProxyDict is reading from ENV | ||
ENV["SOME_RANDOM_KEY"] = "abc" | ||
cfg = dotenv(file) | ||
@test !haskey(cfg.dict, "SOME_RANDOM_KEY") | ||
@test cfg["SOME_RANDOM_KEY"] == "abc" | ||
@test get(cfg, "OTHER_RANDOM_KEY", "zxc") == "zxc" | ||
end | ||
|
||
@testset "Override" begin | ||
# basic input | ||
file = joinpath(dir, ".env.override") | ||
|
||
# By default force override | ||
cfg = dotenv(file) | ||
@test ENV["USER"] == cfg["USER"] | ||
@test ENV["USER"] == "replaced value" | ||
|
||
# Restore previous environment | ||
ENV["USER"] = initial_value | ||
end | ||
|
||
@testset "parsing" begin | ||
|
||
#comment | ||
@test ConfigEnv.parse("#HIMOM") == Dict() | ||
|
||
#spaces without quotes | ||
@test begin | ||
p = ConfigEnv.parse("TEST=hi the re") | ||
count(c -> c == ' ', collect(p["TEST"])) == 4 | ||
@testset "Global" begin | ||
for file in sort([file for file in readdir(@__DIR__) if | ||
occursin(r"^test[_0-9]+.*\.jl$", file)]) | ||
m = match(r"test([0-9]+)_(.*).jl", file) | ||
filename = String(m[2]) | ||
testnum = string(parse(Int, m[1])) | ||
|
||
# with this test one can run only specific tests, for example | ||
# Pkg.test("TimeZoneLookup", test_args = ["xxx"]) | ||
# or | ||
# Pkg.test("TimeZoneLookup", test_args = ["1"]) | ||
if isempty(ARGS) || (filename in ARGS) || (testnum in ARGS) || (m[1] in ARGS) | ||
@testset "$filename" begin | ||
# Here you can optionally exclude some test files | ||
# VERSION < v"1.1" && file == "test_xxx.jl" && continue | ||
|
||
include(file) | ||
end | ||
end | ||
end | ||
|
||
#single quotes | ||
@test ConfigEnv.parse("TEST=''")["TEST"] == "" | ||
@test ConfigEnv.parse("TEST='something'")["TEST"] == "something" | ||
|
||
#double quotes | ||
@test ConfigEnv.parse("TEST=\"\"")["TEST"] == "" | ||
@test ConfigEnv.parse("TEST=\"something\"")["TEST"] == "something" | ||
|
||
#inner quotes are mantained | ||
@test ConfigEnv.parse("TEST=\"\"json\"\"")["TEST"] == "\"json\"" | ||
@test ConfigEnv.parse("TEST=\"'json'\"")["TEST"] == "'json'" | ||
@test ConfigEnv.parse("TEST=\"\"\"")["TEST"] == "\"" | ||
@test ConfigEnv.parse("TEST=\"'\"")["TEST"] == "'" | ||
|
||
#line breaks | ||
@test ConfigEnv.parse("TEST=\"\\n\"")["TEST"] == "" #It's null because of final trim | ||
@test ConfigEnv.parse("TEST=\"\\n\\n\\nsomething\"")["TEST"] == "something" | ||
@test ConfigEnv.parse("TEST=\"something\\nsomething\"")["TEST"] == "something\nsomething" | ||
@test ConfigEnv.parse("TEST=\"something\\n\\nsomething\"")["TEST"] == "something\n\nsomething" | ||
@test ConfigEnv.parse("TEST='\\n'")["TEST"] == "\\n" | ||
@test ConfigEnv.parse("TEST=\\n")["TEST"] == "\\n" | ||
|
||
#empty vars | ||
@test ConfigEnv.parse("TEST=")["TEST"] == "" | ||
|
||
#trim spaces with and without quotes | ||
@test ConfigEnv.parse("TEST=' something '")["TEST"] == "something" | ||
@test ConfigEnv.parse("TEST=\" something \"")["TEST"] == "something" | ||
@test ConfigEnv.parse("TEST= something ")["TEST"] == "something" | ||
@test ConfigEnv.parse("TEST= ")["TEST"] == "" | ||
end | ||
|
||
end # module |
Oops, something went wrong.