From 36725a1a1725764a43d196b1954393dd4071463e Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 8 Sep 2019 21:40:36 -0700 Subject: [PATCH 1/5] Update README.md From 4fba47f20d04690a425df6a4083e2ca430d7d322 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 8 Sep 2019 21:51:38 -0700 Subject: [PATCH 2/5] only build linux binary for now. --- capsule.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/capsule.yml b/capsule.yml index aa363d3..4849d63 100644 --- a/capsule.yml +++ b/capsule.yml @@ -1,12 +1,13 @@ --- + +## - cp /usr/local/osx-ndk-x86/macports/pkgs/opt/local/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc +## - '. /scripts/toolchains/osx/osx-build-env.sh && go build -ldflags "-X main.goos=darwin -X main.goarch=amd64" -o capsulecd-darwin-amd64 -tags "static" $(go list ./cmd/...)' engine_enable_code_mutation: true engine_cmd_compile: - mkdir -p vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/ - - cp /usr/local/osx-ndk-x86/macports/pkgs/opt/local/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc - - '. /scripts/toolchains/osx/osx-build-env.sh && go build -ldflags "-X main.goos=darwin -X main.goarch=amd64" -o capsulecd-darwin-amd64 -tags "static" $(go list ./cmd/...)' - cp /usr/local/linux/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc - '. /scripts/toolchains/linux/linux-build-env.sh && go build -ldflags "-X main.goos=linux -X main.goarch=amd64" -o capsulecd-linux-amd64 -tags "static" $(go list ./cmd/...)' -engine_cmd_test: 'go test -v -tags "static" $(go list ./... | grep -v /vendor/)' +engine_cmd_test: 'go test -v -tags "static" ./...' engine_cmd_lint: 'gometalinter.v2 --vendor --config=gometalinter.json ./...' engine_disable_lint: true @@ -15,5 +16,5 @@ scm_enable_branch_cleanup: true scm_release_assets: - local_path: capsulecd-linux-amd64 artifact_name: capsulecd-linux-amd64 - - local_path: capsulecd-darwin-amd64 - artifact_name: capsulecd-darwin-amd64 +# - local_path: capsulecd-darwin-amd64 +# artifact_name: capsulecd-darwin-amd64 From 3c6458b95698e3fc775982c5ca429e0536a09bf3 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 8 Sep 2019 22:13:17 -0700 Subject: [PATCH 3/5] fixed pkgconfig path. --- capsule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/capsule.yml b/capsule.yml index 4849d63..a6530ad 100644 --- a/capsule.yml +++ b/capsule.yml @@ -5,7 +5,7 @@ engine_enable_code_mutation: true engine_cmd_compile: - mkdir -p vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/ - - cp /usr/local/linux/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc + - cp /usr/local/lib/libgit2/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc - '. /scripts/toolchains/linux/linux-build-env.sh && go build -ldflags "-X main.goos=linux -X main.goarch=amd64" -o capsulecd-linux-amd64 -tags "static" $(go list ./cmd/...)' engine_cmd_test: 'go test -v -tags "static" ./...' engine_cmd_lint: 'gometalinter.v2 --vendor --config=gometalinter.json ./...' From 4fea96ae5f1fb11e80a99246282f1a66101540ce Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 8 Sep 2019 22:36:56 -0700 Subject: [PATCH 4/5] set env variables. --- capsule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/capsule.yml b/capsule.yml index a6530ad..af98419 100644 --- a/capsule.yml +++ b/capsule.yml @@ -6,7 +6,7 @@ engine_enable_code_mutation: true engine_cmd_compile: - mkdir -p vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/ - cp /usr/local/lib/libgit2/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc - - '. /scripts/toolchains/linux/linux-build-env.sh && go build -ldflags "-X main.goos=linux -X main.goarch=amd64" -o capsulecd-linux-amd64 -tags "static" $(go list ./cmd/...)' + - 'export PKG_CONFIG_PATH="/usr/lib/pkgconfig/:/usr/local/lib/pkgconfig/:/usr/local/lib/libgit2/lib/pkgconfig:/usr/local/lib/openssl/lib/pkgconfig:/usr/local/lib/libssh2" && go build -ldflags "-X main.goos=linux -X main.goarch=amd64" -o capsulecd-linux-amd64 -tags "static" $(go list ./cmd/...)' engine_cmd_test: 'go test -v -tags "static" ./...' engine_cmd_lint: 'gometalinter.v2 --vendor --config=gometalinter.json ./...' engine_disable_lint: true From b6a78eabc341bf1937f6c8f9b2d0fff8dfd3e0ab Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Sun, 8 Sep 2019 23:28:46 -0700 Subject: [PATCH 5/5] unset environmental varaibles, and restore them afterwards. --- pkg/config/config_test.go | 5 +++ pkg/utils/env.go | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 pkg/utils/env.go diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 958c00f..b653c7a 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -3,6 +3,7 @@ package config_test import ( "github.com/analogj/capsulecd/pkg/config" "github.com/analogj/capsulecd/pkg/pipeline" + "github.com/analogj/capsulecd/pkg/utils" "github.com/stretchr/testify/require" "os" "path" @@ -12,6 +13,10 @@ import ( func TestConfiguration_init_ShouldCorrectlyInitializeConfiguration(t *testing.T) { t.Parallel() + //setup + defer utils.UnsetEnv("CAPSULE_")() + + //test testConfig, _ := config.Create() diff --git a/pkg/utils/env.go b/pkg/utils/env.go new file mode 100644 index 0000000..8c9ae1f --- /dev/null +++ b/pkg/utils/env.go @@ -0,0 +1,65 @@ +package utils + +import ( + "os" + "strings" +) + +// UnsetEnv unsets all envars having prefix and returns a function +// that restores the env. Any newly added envars having prefix are +// also unset by restore. It is idiomatic to use with a defer. +// +// defer UnsetEnv("ACME_")() +// +// Note that modifying the env may have unpredictable results when +// tests are run with t.Parallel. +// NOTE: This is quick n' dirty from memory; write some tests for +// this code. +func UnsetEnv(prefix string) (restore func()) { + before := map[string]string{} + + for _, e := range os.Environ() { + if !strings.HasPrefix(e, prefix) { + continue + } + + parts := strings.SplitN(e, "=", 2) + before[parts[0]] = parts[1] + + os.Unsetenv(parts[0]) + } + + return func() { + after := map[string]string{} + + for _, e := range os.Environ() { + if !strings.HasPrefix(e, prefix) { + continue + } + + parts := strings.SplitN(e, "=", 2) + after[parts[0]] = parts[1] + + // Check if the envar previously existed + v, ok := before[parts[0]] + if !ok { + // This is a newly added envar with prefix, zap it + os.Unsetenv(parts[0]) + continue + } + + if parts[1] != v { + // If the envar value has changed, set it back + os.Setenv(parts[0], v) + } + } + + // Still need to check if there have been any deleted envars + for k, v := range before { + if _, ok := after[k]; !ok { + // k is not present in after, so we set it. + os.Setenv(k, v) + } + } + } +}