Skip to content

Commit ae490ab

Browse files
committed
test: assert file contents
1 parent ed80e34 commit ae490ab

2 files changed

Lines changed: 4 additions & 44 deletions

File tree

cmd/env/add_test.go

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -205,57 +205,17 @@ func Test_Env_AddCommand(t *testing.T) {
205205
},
206206
},
207207
"add a variable to the .env file for non-hosted app": {
208-
CmdArgs: []string{"ENV_NAME", "ENV_VALUE"},
209-
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
210-
setupEnvAddDotenvMocks(ctx, cm, cf)
211-
},
212-
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
213-
cm.API.AssertNotCalled(t, "AddVariable")
214-
cm.IO.AssertCalled(
215-
t,
216-
"PrintTrace",
217-
mock.Anything,
218-
slacktrace.EnvAddSuccess,
219-
mock.Anything,
220-
)
221-
},
222-
},
223-
"add a variable preserving existing variables and comments in .env": {
224208
CmdArgs: []string{"NEW_VAR", "new_value"},
225209
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
226210
setupEnvAddDotenvMocks(ctx, cm, cf)
227-
err := afero.WriteFile(cf.Fs, ".env", []byte("# Config\nEXISTING=value\n"), 0644)
211+
err := afero.WriteFile(cf.Fs, ".env", []byte("# Config\nEXISTING=value\n"), 0600)
228212
assert.NoError(t, err)
229213
},
230214
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
231215
cm.API.AssertNotCalled(t, "AddVariable")
232-
},
233-
},
234-
"overwrite an existing variable in .env file": {
235-
CmdArgs: []string{"MY_VAR", "new_value"},
236-
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
237-
setupEnvAddDotenvMocks(ctx, cm, cf)
238-
err := afero.WriteFile(cf.Fs, ".env", []byte("MY_VAR=old_value\n"), 0644)
216+
content, err := afero.ReadFile(cm.Fs, ".env")
239217
assert.NoError(t, err)
240-
},
241-
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
242-
cm.API.AssertNotCalled(t, "AddVariable")
243-
},
244-
},
245-
"create .env file when it does not exist": {
246-
CmdArgs: []string{"FIRST_VAR", "first_value"},
247-
Setup: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock, cf *shared.ClientFactory) {
248-
setupEnvAddDotenvMocks(ctx, cm, cf)
249-
},
250-
ExpectedAsserts: func(t *testing.T, ctx context.Context, cm *shared.ClientsMock) {
251-
cm.API.AssertNotCalled(t, "AddVariable")
252-
cm.IO.AssertCalled(
253-
t,
254-
"PrintTrace",
255-
mock.Anything,
256-
slacktrace.EnvAddSuccess,
257-
mock.Anything,
258-
)
218+
assert.Equal(t, "# Config\nEXISTING=value\nNEW_VAR=\"new_value\"\n", string(content))
259219
},
260220
},
261221
}, func(cf *shared.ClientFactory) *cobra.Command {

internal/slackdotenv/slackdotenv_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func Test_Set(t *testing.T) {
195195
t.Run(name, func(t *testing.T) {
196196
fs := afero.NewMemMapFs()
197197
if tc.writeExisting {
198-
err := afero.WriteFile(fs, ".env", []byte(tc.existingEnv), 0644)
198+
err := afero.WriteFile(fs, ".env", []byte(tc.existingEnv), 0600)
199199
assert.NoError(t, err)
200200
}
201201
err := Set(fs, tc.name, tc.value)

0 commit comments

Comments
 (0)