Skip to content

Commit 0a218e1

Browse files
Use afero helper to update .talismanrc file
Authored-by: Owen Nelson <[email protected]>
1 parent 8cb7b78 commit 0a218e1

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

talismanrc/rc_file.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ func talismanRCFromYaml(fileContents []byte) (*TalismanRC, error) {
4444
return &talismanRCFromFile, nil
4545
}
4646

47+
func (tRC *TalismanRC) saveToFile() {
48+
ignoreEntries, _ := yaml.Marshal(&tRC)
49+
err := afero.WriteFile(fs, currentRCFileName, ignoreEntries, 0644)
50+
if err != nil {
51+
logr.Errorf("error writing to %s: %s", currentRCFileName, err)
52+
}
53+
}
54+
4755
func SetFs__(_fs afero.Fs) {
4856
fs = _fs
4957
}

talismanrc/rc_file_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ func TestLoadingFromFile(t *testing.T) {
4949
SetRcFilename__(DefaultRCFileName)
5050
}
5151

52+
func TestWritingToFile(t *testing.T) {
53+
tRC := &TalismanRC{Version: DefaultRCVersion}
54+
fs := afero.NewMemMapFs()
55+
SetFs__(fs)
56+
57+
t.Run("When there is no .talismanrc file", func(t *testing.T) {
58+
talismanRCFileExists, _ := afero.Exists(fs, DefaultRCFileName)
59+
assert.False(t, talismanRCFileExists, "Problem setting up tests")
60+
tRC.saveToFile()
61+
talismanRCFileExists, _ = afero.Exists(fs, DefaultRCFileName)
62+
assert.True(t, talismanRCFileExists, "Should have created a new .talismanrc file")
63+
fileContents, _ := afero.ReadFile(fs, DefaultRCFileName)
64+
assert.Equal(t, "version: \"1.0\"\n", string(fileContents))
65+
})
66+
67+
t.Run("When there already is a .talismanrc file", func(t *testing.T) {
68+
err := afero.WriteFile(fs, DefaultRCFileName, []byte("Some existing content to overwrite"), 0666)
69+
assert.NoError(t, err, "Problem setting up tests")
70+
tRC.saveToFile()
71+
talismanRCFileExists, _ := afero.Exists(fs, DefaultRCFileName)
72+
assert.True(t, talismanRCFileExists, "Should have created a new .talismanrc file")
73+
fileContents, _ := afero.ReadFile(fs, DefaultRCFileName)
74+
assert.Equal(t, "version: \"1.0\"\n", string(fileContents))
75+
})
76+
}
77+
5278
func TestUnmarshalsValidYaml(t *testing.T) {
5379
t.Run("Should not fail as long as the yaml structure is correct", func(t *testing.T) {
5480
fileContents := []byte(`

talismanrc/talismanrc.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package talismanrc
22

33
import (
4-
"os"
54
"sort"
65

76
logr "github.com/sirupsen/logrus"
@@ -62,23 +61,7 @@ func (tRC *TalismanRC) AddIgnores(entriesToAdd []FileIgnoreConfig) {
6261
if len(entriesToAdd) > 0 {
6362
logr.Debugf("Adding entries: %v", entriesToAdd)
6463
tRC.FileIgnoreConfig = combineFileIgnores(tRC.FileIgnoreConfig, entriesToAdd)
65-
66-
ignoreEntries, _ := yaml.Marshal(&tRC)
67-
file, err := fs.OpenFile(currentRCFileName, os.O_CREATE|os.O_WRONLY, 0644)
68-
if err != nil {
69-
logr.Errorf("error opening %s: %s", currentRCFileName, err)
70-
}
71-
defer func() {
72-
err := file.Close()
73-
if err != nil {
74-
logr.Errorf("error closing %s: %s", currentRCFileName, err)
75-
}
76-
}()
77-
logr.Debugf("Writing talismanrc: %v", string(ignoreEntries))
78-
_, err = file.WriteString(string(ignoreEntries))
79-
if err != nil {
80-
logr.Errorf("error writing to %s: %s", currentRCFileName, err)
81-
}
64+
tRC.saveToFile()
8265
}
8366
}
8467

talismanrc/talismanrc_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,15 @@ func TestAddingFileIgnores(t *testing.T) {
136136
ignoreConfig := FileIgnoreConfig{
137137
FileName: "Foo",
138138
Checksum: "SomeCheckSum"}
139-
t.Run("When .talismanrc doesn't exist yet", func(t *testing.T) {
140-
139+
t.Run("When .talismanrc is empty", func(t *testing.T) {
141140
initialRCConfig, _ := Load()
142141
initialRCConfig.AddIgnores([]FileIgnoreConfig{ignoreConfig})
143142
newRCConfig, _ := Load()
144143
assert.Equal(t, 1, len(newRCConfig.FileIgnoreConfig))
145144
_ = fs.Remove(ignoreFile)
146145
})
147146

148-
t.Run("When there already is a .talismanrc", func(t *testing.T) {
147+
t.Run("When .talismanrc has lots of configurations", func(t *testing.T) {
149148
err = afero.WriteFile(fs, ignoreFile, []byte(fullyConfiguredTalismanRC), 0666)
150149
assert.NoError(t, err)
151150

0 commit comments

Comments
 (0)