Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
Merge b6a78ea into f1b8241
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogJ authored Sep 9, 2019
2 parents f1b8241 + b6a78ea commit b44783e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
15 changes: 8 additions & 7 deletions capsule.yml
Original file line number Diff line number Diff line change
@@ -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/)'
- cp /usr/local/lib/libgit2/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc
- '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

Expand All @@ -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
5 changes: 5 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,6 +13,10 @@ import (
func TestConfiguration_init_ShouldCorrectlyInitializeConfiguration(t *testing.T) {
t.Parallel()

//setup
defer utils.UnsetEnv("CAPSULE_")()


//test
testConfig, _ := config.Create()

Expand Down
65 changes: 65 additions & 0 deletions pkg/utils/env.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit b44783e

Please sign in to comment.