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

Commit b44783e

Browse files
authored
Merge b6a78ea into f1b8241
2 parents f1b8241 + b6a78ea commit b44783e

3 files changed

Lines changed: 78 additions & 7 deletions

File tree

capsule.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
---
2+
3+
## - 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
4+
## - '. /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/...)'
25
engine_enable_code_mutation: true
36
engine_cmd_compile:
47
- mkdir -p vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/
5-
- 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
6-
- '. /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/...)'
7-
- cp /usr/local/linux/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc
8-
- '. /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/...)'
9-
engine_cmd_test: 'go test -v -tags "static" $(go list ./... | grep -v /vendor/)'
8+
- cp /usr/local/lib/libgit2/lib/pkgconfig/libgit2.pc vendor/gopkg.in/libgit2/git2go.v25/vendor/libgit2/build/libgit2.pc
9+
- '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/...)'
10+
engine_cmd_test: 'go test -v -tags "static" ./...'
1011
engine_cmd_lint: 'gometalinter.v2 --vendor --config=gometalinter.json ./...'
1112
engine_disable_lint: true
1213

@@ -15,5 +16,5 @@ scm_enable_branch_cleanup: true
1516
scm_release_assets:
1617
- local_path: capsulecd-linux-amd64
1718
artifact_name: capsulecd-linux-amd64
18-
- local_path: capsulecd-darwin-amd64
19-
artifact_name: capsulecd-darwin-amd64
19+
# - local_path: capsulecd-darwin-amd64
20+
# artifact_name: capsulecd-darwin-amd64

pkg/config/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config_test
33
import (
44
"github.com/analogj/capsulecd/pkg/config"
55
"github.com/analogj/capsulecd/pkg/pipeline"
6+
"github.com/analogj/capsulecd/pkg/utils"
67
"github.com/stretchr/testify/require"
78
"os"
89
"path"
@@ -12,6 +13,10 @@ import (
1213
func TestConfiguration_init_ShouldCorrectlyInitializeConfiguration(t *testing.T) {
1314
t.Parallel()
1415

16+
//setup
17+
defer utils.UnsetEnv("CAPSULE_")()
18+
19+
1520
//test
1621
testConfig, _ := config.Create()
1722

pkg/utils/env.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
// UnsetEnv unsets all envars having prefix and returns a function
9+
// that restores the env. Any newly added envars having prefix are
10+
// also unset by restore. It is idiomatic to use with a defer.
11+
//
12+
// defer UnsetEnv("ACME_")()
13+
//
14+
// Note that modifying the env may have unpredictable results when
15+
// tests are run with t.Parallel.
16+
// NOTE: This is quick n' dirty from memory; write some tests for
17+
// this code.
18+
func UnsetEnv(prefix string) (restore func()) {
19+
before := map[string]string{}
20+
21+
for _, e := range os.Environ() {
22+
if !strings.HasPrefix(e, prefix) {
23+
continue
24+
}
25+
26+
parts := strings.SplitN(e, "=", 2)
27+
before[parts[0]] = parts[1]
28+
29+
os.Unsetenv(parts[0])
30+
}
31+
32+
return func() {
33+
after := map[string]string{}
34+
35+
for _, e := range os.Environ() {
36+
if !strings.HasPrefix(e, prefix) {
37+
continue
38+
}
39+
40+
parts := strings.SplitN(e, "=", 2)
41+
after[parts[0]] = parts[1]
42+
43+
// Check if the envar previously existed
44+
v, ok := before[parts[0]]
45+
if !ok {
46+
// This is a newly added envar with prefix, zap it
47+
os.Unsetenv(parts[0])
48+
continue
49+
}
50+
51+
if parts[1] != v {
52+
// If the envar value has changed, set it back
53+
os.Setenv(parts[0], v)
54+
}
55+
}
56+
57+
// Still need to check if there have been any deleted envars
58+
for k, v := range before {
59+
if _, ok := after[k]; !ok {
60+
// k is not present in after, so we set it.
61+
os.Setenv(k, v)
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)