Skip to content

Commit

Permalink
refactor: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrombley committed Jul 7, 2021
1 parent 9c4165a commit 930a22d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 176 deletions.
160 changes: 0 additions & 160 deletions cmd/newrelic/command_integration_test.go

This file was deleted.

7 changes: 6 additions & 1 deletion internal/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ var (
)

func init() {
Init(configBasePath())
}

func Init(basePath string) {
BasePath = basePath
initializeConfigProvider()
initializeCredentialsProvider()
}
Expand Down Expand Up @@ -98,7 +103,7 @@ func initializeConfigProvider() {
FieldDefinition{
Key: PluginDir,
EnvVar: "NEW_RELIC_CLI_PLUGIN_DIR",
Default: filepath.Join(configBasePath(), pluginDir),
Default: filepath.Join(BasePath, pluginDir),
},
FieldDefinition{
Key: PreReleaseFeatures,
Expand Down
8 changes: 4 additions & 4 deletions internal/configuration/config_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestConfigSetLogLevel(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(f)

BasePath = f
Init(f)

// Set the valid log levels
for _, l := range []string{
Expand All @@ -41,7 +41,7 @@ func TestConfigSetSendUsageData(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(f)

BasePath = f
Init(f)

// Set the valid sendUsageData values
for _, l := range []Ternary{
Expand All @@ -63,7 +63,7 @@ func TestConfigSetPreReleaseFeatures(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(f)

BasePath = f
Init(f)

// Set the valid pre-release feature values
for _, l := range []Ternary{
Expand All @@ -85,7 +85,7 @@ func TestConfigSetPluginDir(t *testing.T) {
assert.NoError(t, err)
defer os.RemoveAll(f)

BasePath = f
Init(f)

err = SetConfigString(PluginDir, "test")
assert.NoError(t, err)
Expand Down
4 changes: 3 additions & 1 deletion internal/configuration/config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ func (p *ConfigProvider) SetWithScope(scope string, key ConfigKey, value interfa
return fmt.Errorf("key '%s' is not valid, valid keys are: %v", key, p.getConfigValueKeys())
}

cfg := p.getConfig()

p.mu.Lock()
defer p.mu.Unlock()

cfg, err := sjson.Set(p.getConfig(), escapeWildcards(p.getPath(scope, key)), value)
cfg, err := sjson.Set(cfg, escapeWildcards(p.getPath(scope, key)), value)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/configuration/config_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//build +integration
//+build integration

package configuration

Expand Down
14 changes: 7 additions & 7 deletions internal/configuration/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestGetActiveProfileValues(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

BasePath = dir
Init(dir)

err = ioutil.WriteFile(filepath.Join(dir, credentialsFileName), []byte(testCredentials), 0644)
require.NoError(t, err)
Expand All @@ -61,7 +61,7 @@ func TestGetActiveProfileValues_EnvVarOverride(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

BasePath = dir
Init(dir)

err = ioutil.WriteFile(filepath.Join(dir, credentialsFileName), []byte(testCredentials), 0644)
require.NoError(t, err)
Expand All @@ -86,7 +86,7 @@ func TestGetConfigValues(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

BasePath = dir
Init(dir)

err = ioutil.WriteFile(filepath.Join(dir, configFileName), []byte(testConfig), 0644)
require.NoError(t, err)
Expand All @@ -109,7 +109,7 @@ func TestGetConfigValues_EnvVarOverride(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

BasePath = dir
Init(dir)

err = ioutil.WriteFile(filepath.Join(dir, credentialsFileName), []byte(testConfig), 0644)
require.NoError(t, err)
Expand All @@ -132,7 +132,7 @@ func TestGetConfigValues_DefaultValues(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

BasePath = dir
Init(dir)

os.Unsetenv("NEW_RELIC_CLI_LOG_LEVEL")
os.Unsetenv("NEW_RELIC_CLI_PLUGIN_DIR")
Expand All @@ -142,7 +142,7 @@ func TestGetConfigValues_DefaultValues(t *testing.T) {
initializeConfigProvider()

require.Equal(t, "info", GetConfigString("loglevel"))
require.Equal(t, filepath.Join(configBasePath(), pluginDir), GetConfigString("plugindir"))
require.Equal(t, filepath.Join(dir, pluginDir), GetConfigString("plugindir"))
require.Equal(t, "NOT_ASKED", GetConfigString("prereleasefeatures"))
require.Equal(t, "NOT_ASKED", GetConfigString("sendusagedata"))
}
Expand All @@ -152,7 +152,7 @@ func TestRemoveProfile(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

BasePath = dir
Init(dir)

filename := filepath.Join(dir, credentialsFileName)
err = ioutil.WriteFile(filename, []byte(testCredentials), 0644)
Expand Down
4 changes: 2 additions & 2 deletions internal/install/recipes/local_recipe_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestLocalRecipeFetcher_FetchRecipes_EmptyManifest(t *testing.T) {
defer os.RemoveAll(tmp)
require.NoError(t, err)

configuration.BasePath = tmp
configuration.Init(tmp)

path := filepath.Join(tmp, "recipes")
err = os.MkdirAll(path, 0750)
Expand All @@ -40,7 +40,7 @@ func TestLocalRecipeFetcher_FetchRecipes(t *testing.T) {
defer os.RemoveAll(tmp)
require.NoError(t, err)

configuration.BasePath = tmp
configuration.Init(tmp)

path := filepath.Join(tmp, "recipes")

Expand Down

0 comments on commit 930a22d

Please sign in to comment.