Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: occasional panic when watched YAML files change #1246

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/pkg/sync/file/filepath_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func (fs *Sync) fetch(_ context.Context) (string, error) {
// yamlToJSON is a generic helper function to convert
// yaml to json
func yamlToJSON(rawFile []byte) (string, error) {
if len(rawFile) == 0 {
return "", nil
}

var ms map[string]interface{}
// yaml.Unmarshal unmarshals to map[interface]interface{}
if err := yaml.Unmarshal(rawFile, &ms); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions core/pkg/sync/file/filepath_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,31 @@ func writeToFile(t *testing.T, fetchDirName, fileContents string) {
t.Fatal(err)
}
}

func TestFilePathSync_yamlToJSON(t *testing.T) {
tests := map[string]struct {
input []byte
handleResponse func(t *testing.T, output string, err error)
}{
"empty": {
input: []byte(""),
handleResponse: func(t *testing.T, output string, err error) {
if err != nil {
t.Fatalf("expect no err, got err = %v", err)
}

if output != "" {
t.Fatalf("expect output = '', got output = '%v'", output)
}
},
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
output, err := yamlToJSON(tt.input)

tt.handleResponse(t, output, err)
})
}
}
Loading