-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement 'unset' subcommand * code review fix: 'unset' option ignored when reading from envstore file * code review fix: use explicitly declared default value
- Loading branch information
Showing
8 changed files
with
252 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package integration | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/bitrise-io/go-utils/command" | ||
"github.com/bitrise-io/go-utils/pathutil" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func unsetCommand(key, envstore string) *command.Model { | ||
return command.New(binPath(), "-p", envstore, "unset", "--key", key) | ||
} | ||
|
||
func runCommand(cmd, envstore string) *command.Model { | ||
return command.New(binPath(), "-p", envstore, "run", cmd) | ||
} | ||
|
||
func TestUnset(t *testing.T) { | ||
t.Log("only unset on an empty envstore") | ||
{ | ||
// create a fully empty envstore | ||
tmpDir, err := pathutil.NormalizedOSTempDirPath("__envman__") | ||
require.NoError(t, err) | ||
|
||
envstore := filepath.Join(tmpDir, ".envstore") | ||
f, err := os.Create(envstore) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
randomEnvKEY := "DONOTEXPORT" | ||
|
||
// unset DONOTEXPORT env | ||
out, err := unsetCommand(randomEnvKEY, envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// run env command through envman and see the exported env's list | ||
out, err = runCommand("env", envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// check if the env is surely not exported | ||
if strings.Contains(out, randomEnvKEY) { | ||
t.Errorf("env is exported however it should be unset, complete list of exported envs:\n%s\n", out) | ||
} | ||
} | ||
|
||
t.Log("add env then unset it in an empty envstore") | ||
{ | ||
// create a fully empty envstore | ||
tmpDir, err := pathutil.NormalizedOSTempDirPath("__envman__") | ||
require.NoError(t, err) | ||
|
||
envstore := filepath.Join(tmpDir, ".envstore") | ||
f, err := os.Create(envstore) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
randomEnvKEY := "DONOTEXPORT" | ||
|
||
// add DONOTEXPORT env | ||
out, err := addCommand(randomEnvKEY, "sample value", envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// unset DONOTEXPORT env | ||
out, err = unsetCommand(randomEnvKEY, envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// run env command through envman and see the exported env's list | ||
out, err = runCommand("env", envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// check if the env is surely not exported | ||
if strings.Contains(out, randomEnvKEY) { | ||
t.Errorf("env is exported however it should be unset, complete list of exported envs:\n%s\n", out) | ||
} | ||
} | ||
|
||
t.Log("set env externally then only unset on an empty envstore") | ||
{ | ||
// create a fully empty envstore | ||
tmpDir, err := pathutil.NormalizedOSTempDirPath("__envman__") | ||
require.NoError(t, err) | ||
|
||
envstore := filepath.Join(tmpDir, ".envstore") | ||
f, err := os.Create(envstore) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
randomEnvKEY := "DONOTEXPORT" | ||
|
||
require.NoError(t, os.Setenv(randomEnvKEY, "value")) | ||
|
||
// unset DONOTEXPORT env | ||
out, err := unsetCommand(randomEnvKEY, envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// run env command through envman and see the exported env's list | ||
out, err = runCommand("env", envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// check if the env is surely not exported | ||
if strings.Contains(out, randomEnvKEY) { | ||
t.Errorf("env is exported however it should be unset, complete list of exported envs:\n%s\n", out) | ||
} | ||
} | ||
|
||
t.Log("set env externally then add env then unset it in an empty envstore") | ||
{ | ||
// create a fully empty envstore | ||
tmpDir, err := pathutil.NormalizedOSTempDirPath("__envman__") | ||
require.NoError(t, err) | ||
|
||
envstore := filepath.Join(tmpDir, ".envstore") | ||
f, err := os.Create(envstore) | ||
require.NoError(t, err) | ||
require.NoError(t, f.Close()) | ||
|
||
controlEnvKey := "EXPORT_THIS" | ||
randomEnvKEY := "DONOTEXPORT" | ||
|
||
require.NoError(t, os.Setenv(controlEnvKey, "value")) | ||
require.NoError(t, os.Setenv(randomEnvKEY, "value")) | ||
|
||
// add DONOTEXPORT env | ||
out, err := addCommand(randomEnvKEY, "sample value", envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// unset DONOTEXPORT env | ||
out, err = unsetCommand(randomEnvKEY, envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// run env command through envman and see the exported env's list | ||
out, err = runCommand("env", envstore).RunAndReturnTrimmedCombinedOutput() | ||
require.NoError(t, err, out) | ||
|
||
// check if the env is surely not exported | ||
if !strings.Contains(out, controlEnvKey) { | ||
t.Errorf("env %s is not exported, complete list of exported envs:\n%s\n", controlEnvKey, out) | ||
} | ||
|
||
// check if the env is surely not exported | ||
if strings.Contains(out, randomEnvKEY) { | ||
t.Errorf("env is exported however it should be unset, complete list of exported envs:\n%s\n", out) | ||
} | ||
} | ||
} |
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 cli | ||
|
||
import ( | ||
"github.com/bitrise-io/envman/envman" | ||
"github.com/bitrise-io/envman/models" | ||
"github.com/bitrise-io/go-utils/pointers" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
func unset(c *cli.Context) error { | ||
key := c.String(KeyKey) | ||
// Load envs, or create if not exist | ||
environments, err := envman.ReadEnvsOrCreateEmptyList() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Add or update envlist | ||
newEnv := models.EnvironmentItemModel{ | ||
key: "", | ||
models.OptionsKey: models.EnvironmentItemOptionsModel{ | ||
Unset: pointers.NewBoolPtr(true), | ||
}, | ||
} | ||
|
||
if err := newEnv.NormalizeValidateFillDefaults(); err != nil { | ||
return err | ||
} | ||
|
||
newEnvSlice, err := envman.UpdateOrAddToEnvlist(environments, newEnv, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return envman.WriteEnvMapToFile(envman.CurrentEnvStoreFilePath, newEnvSlice) | ||
} |
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