Skip to content

Commit

Permalink
fix: propagate any errors in src/pkg/variables
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp committed Sep 25, 2024
1 parent 7c04dff commit acdb601
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/pkg/variables/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (vc *VariableConfig) ReplaceTextTemplate(path string) error {
if err != nil {
return err
}
defer textFile.Close()

// This regex takes a line and parses the text before and after a discovered template: https://regex101.com/r/ilUxAz/1
regexTemplateLine := regexp.MustCompile(fmt.Sprintf("(?P<preTemplate>.*?)(?P<template>%s)(?P<postTemplate>.*)", templateRegex))
Expand Down Expand Up @@ -130,5 +129,11 @@ func (vc *VariableConfig) ReplaceTextTemplate(path string) error {
}
}

// We're done scanning, close our file
err = textFile.Close()
if err != nil {
return err
}

return os.WriteFile(path, []byte(text), helpers.ReadWriteUser)
}
11 changes: 7 additions & 4 deletions src/pkg/variables/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ func TestReplaceTextTemplate(t *testing.T) {
tmpDir := t.TempDir()
tc.path = filepath.Join(tmpDir, "templates.test")

f, _ := os.Create(tc.path)
defer f.Close()
f, err := os.Create(tc.path)
require.NoError(t, err)

_, err := f.WriteString(start)
_, err = f.WriteString(start)
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
}

Expand All @@ -145,7 +147,8 @@ func TestReplaceTextTemplate(t *testing.T) {
require.Error(t, gotErr)
} else {
require.NoError(t, gotErr)
gotContents, _ := os.ReadFile(tc.path)
gotContents, err := os.ReadFile(tc.path)
require.NoError(t, err)
require.Equal(t, tc.wantContents, string(gotContents))
}
}
Expand Down

0 comments on commit acdb601

Please sign in to comment.