Skip to content

Commit

Permalink
Only trim quotes from exported env var if there are quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkutner committed May 9, 2019
1 parent 67411ae commit 8202cff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
8 changes: 6 additions & 2 deletions exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func DumpExportsFile(exportsData, envDir string) error {

for _, line := range lines {
if found, key, value := ParseExportsFileLine(line); found {
err := WriteEnvFile(envDir, key, value[1 : len(value)-1])
err := WriteEnvFile(envDir, key, value)
if err != nil {
return err
}
Expand All @@ -24,7 +24,11 @@ func ParseExportsFileLine(line string) (bool, string, string) {
components := strings.Split(line, "=")
export := strings.Split(components[0], " ")
if strings.TrimSpace(export[0]) == "export" {
return true, strings.TrimSpace(export[1]), components[1]
val := strings.TrimSpace(components[1])
if string(val[0]) == "\"" && string(val[len(val)-1]) == "\"" {
val = val[1: len(val)-1]
}
return true, strings.TrimSpace(export[1]), val
} else {
return false, "", ""
}
Expand Down
46 changes: 46 additions & 0 deletions exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,52 @@ export FOO=bar
t.Error(err.Error())
}

if strings.TrimSpace(string(actual)) != expected {
t.Errorf("Expected %s; got %s", expected, actual)
}
}

func TestDumpExportsFileWithQuotes(t *testing.T) {
envDir, err := ioutil.TempDir("", "env")

exports := `
export FOO="bar"
`
err = cnbshim.DumpExportsFile(exports, envDir)
if err != nil {
t.Error(err.Error())
}

expectedFile := filepath.Join(envDir, "FOO")
expected := "bar"
actual, err := ioutil.ReadFile(expectedFile)
if err != nil {
t.Error(err.Error())
}

if strings.TrimSpace(string(actual)) != expected {
t.Errorf("Expected %s; got %s", expected, actual)
}
}

func TestDumpExportsFileWithEscapedQuotes(t *testing.T) {
envDir, err := ioutil.TempDir("", "env")

exports := `
export FOO="b\"a\"r"
`
err = cnbshim.DumpExportsFile(exports, envDir)
if err != nil {
t.Error(err.Error())
}

expectedFile := filepath.Join(envDir, "FOO")
expected := "b\\\"a\\\"r"
actual, err := ioutil.ReadFile(expectedFile)
if err != nil {
t.Error(err.Error())
}

if strings.TrimSpace(string(actual)) != expected {
t.Errorf("Expected %s; got %s", expected, actual)
}
Expand Down

0 comments on commit 8202cff

Please sign in to comment.