-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved exports stuff into top level and added tests
- Loading branch information
Showing
9 changed files
with
111 additions
and
38 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 |
---|---|---|
|
@@ -5,4 +5,5 @@ target | |
*.iml | ||
.DS_Store | ||
bin/release | ||
bin/exports | ||
cnb-shim-*.tgz |
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cnbshim | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func DumpExportsFile(exportsData, envDir string) error { | ||
lines := strings.SplitN(exportsData, "\n", 2) | ||
|
||
for _, line := range lines { | ||
if found, key, value := ParseExportsFileLine(line); found { | ||
err := WriteEnvFile(envDir, key, value) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
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] | ||
} else { | ||
return false, "", "" | ||
} | ||
|
||
} | ||
|
||
func WriteEnvFile(envDir, filename, value string) error { | ||
return ioutil.WriteFile(filepath.Join(envDir, filename), []byte(value), 0644) | ||
} |
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,57 @@ | ||
package cnbshim_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/heroku/cnb-shim" | ||
) | ||
|
||
func TestWriteEnvFile(t *testing.T) { | ||
envDir, err := ioutil.TempDir("", "env") | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFilename := "FOO" | ||
expected := "bar" | ||
err = cnbshim.WriteEnvFile(envDir, expectedFilename, expected) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
expectedFile := filepath.Join(envDir, expectedFilename) | ||
actual, err := ioutil.ReadFile(expectedFile) | ||
if err != nil { | ||
t.Error(err.Error()) | ||
} | ||
|
||
if string(actual) != expected { | ||
t.Errorf("Expected %s; got %s", expected, actual) | ||
} | ||
} | ||
|
||
func TestDumpExportsFile(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) | ||
} | ||
} |
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,4 +1,4 @@ | ||
package releaser | ||
package cnbshim | ||
|
||
import ( | ||
"io/ioutil" | ||
|
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