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 parsing empty yaml documents #228

Merged
merged 2 commits into from
Feb 14, 2023
Merged

Fix parsing empty yaml documents #228

merged 2 commits into from
Feb 14, 2023

Conversation

Zebradil
Copy link
Member

@Zebradil Zebradil commented Feb 5, 2023

Fixes #201

Signed-off-by: German Lashevich <[email protected]>
Copy link
Member

@praveenrewar praveenrewar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@joaopapereira joaopapereira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind adding a test to ensure that this feature is working?

@Zebradil
Copy link
Member Author

Zebradil commented Feb 9, 2023

@joaopapereira

Do you mind adding a test to ensure that this feature is working?

Disclaimer: I'm not a golang developer.

I was thinking about writing a test before implementing the fix, but I couldn't figure out how to do that nicely and without restructuring the existing code. So maybe I can get some advice here.

This is the function which would be tested:

func parseResources(paths []string, resourceFunc func([]byte) error) error

It accepts a list of file paths or - for stdin and a callback function and returns an error.

In a test, I can create a temporary file with content suitable for the test, pass the path to the function together with a callback function and check if parseResources returns an error.

This doesn't seem to be elegant but should work. Or maybe there are other ideas?

@neil-hickey
Copy link

👋 Hello, I wrote a quick test you can copy verbatim or not and use as inspiration to help move this issue along:

New file pkg/vendir/config/config_test.go

func TestEmptyLineStartsConfig(t *testing.T) {
	t.Run("whitespace in config is ignored", func(t *testing.T) {
		tempConfigPath := filepath.Join(t.TempDir(), "config.yml")
		configWithWhitespace := []byte(`
---
apiVersion: v1
kind: Secret
metadata:
  name: vendir-secret
data:
  username: dXNlcg==
  password: MTIzNDU2`)

		require.NoError(t, os.WriteFile(tempConfigPath, configWithWhitespace, 0666))

		_, _, _, err := config.NewConfigFromFiles([]string{tempConfigPath})
		require.NoError(t, err)
	})
}

@Zebradil
Copy link
Member Author

@neil-hickey awesome, thanks!

I've added the test.

It fails on the develop branch:

--- FAIL: TestEmptyLineStartsConfig (0.00s)
    --- FAIL: TestEmptyLineStartsConfig/whitespace_in_config_is_ignored (0.00s)
        config_test.go:27:
                Error Trace:    /home/zebradil/development/forks/vendir/pkg/vendir/config/config_test.go:27
                Error:          Received unexpected error:
                                Parsing resource config '/tmp/TestEmptyLineStartsConfigwhitespace_in_config_is_ignored185744596/001/config.yml': Unknown apiVersion '' or kind '' for resource
                Test:           TestEmptyLineStartsConfig/whitespace_in_config_is_ignored
FAIL
FAIL    command-line-arguments  0.004s
FAIL

It passes on the feature branch:

ok      command-line-arguments  0.004s

@Zebradil Zebradil requested review from joaopapereira and removed request for cppforlife February 13, 2023 23:56
Copy link
Member

@joaopapereira joaopapereira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@joaopapereira joaopapereira merged commit 1ee548c into carvel-dev:develop Feb 14, 2023
@Zebradil Zebradil deleted the fix/201-parsing-empty-yaml-documents branch October 11, 2023 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Error parsing empty yaml documents
4 participants