Skip to content

test: add test for options env #161

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

Merged
merged 7 commits into from
Apr 30, 2024
Merged

test: add test for options env #161

merged 7 commits into from
Apr 30, 2024

Conversation

BrunoQuaresma
Copy link
Contributor

After some thinking, I concluded that we don't need to test all the env vars but how those values are parsed, but let me know if you think we should test all the env variables.

Closes #157

@BrunoQuaresma BrunoQuaresma self-assigned this Apr 29, 2024
@BrunoQuaresma BrunoQuaresma marked this pull request as ready for review April 29, 2024 15:20
options_test.go Outdated
t.Setenv("SETUP_SCRIPT", "setup.sh")
t.Setenv("CACHE_TTL_DAYS", "7")
t.Setenv("IGNORE_PATHS", "/var,/tmp")
t.Setenv("SKIP_REBUILD", "true")
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous code was using https://pkg.go.dev/strconv#ParseBool, but I'm not sure what the new code is using so we might need to test 1/0/true/false (and the latter two's capitalised equivalents).

@Emyrk
Copy link
Member

Emyrk commented Apr 29, 2024

The runCLI is doing a lot of extra work, where this is really just testing env var assignment. An option instead is to skip the cli part, and just do the env parsing:

	t.Run("string", func(t *testing.T) {
		var o envbuilder.Options
		set := o.CLI()
		err := set.ParseEnv([]serpent.EnvVar{
			{"SETUP_SCRIPT", "setup.sh"},
			{"INIT_COMMAND", "sleep infinity"},
		})
		require.NoError(t, err)

		require.Equal(t, o.SetupScript, "setup.sh")
		require.Equal(t, o.InitCommand, "sleep infinity")
	})

If we do this, we can also just merge all the tests. But one thing that feels off here is that we are only testing the happy paths, which makes this test feel weak in general.

We could comment this is just a sanity check to make sure we don't change env vars without documenting it to prevent any breaking changes. In that case, I think merging the tests and doing it the method above would be a bit cleaner.

// TestEnvOptionParsing prevents breaking changes to the environment variables.
func TestEnvOptionParsing(t *testing.T) {
	t.Parallel()

	var o envbuilder.Options
	set := o.CLI()
	err := set.ParseEnv([]serpent.EnvVar{
		{"SETUP_SCRIPT", "setup.sh"},
		{"INIT_COMMAND", "sleep infinity"},
		// ... Add the rest
		{"CACHE_TTL_DAYS", "7"},
	})
	require.NoError(t, err)

	require.Equal(t, o.SetupScript, "setup.sh")
	require.Equal(t, o.InitCommand, "sleep infinity")
 	// ... Add the rest
	require.Equal(t, o.CacheTTLDays, int64(7))
}

@BrunoQuaresma
Copy link
Contributor Author

@Emyrk I think it makes sense. Since it is a straightforward test I don't see how to test "unhappy" paths, how would you do that in this scenario?

@Emyrk
Copy link
Member

Emyrk commented Apr 29, 2024

@Emyrk I think it makes sense. Since it is a straightforward test I don't see how to test "unhappy" paths, how would you do that in this scenario?

I don't think it is worth it to test unhappy paths, it does feel excessive. Just trying to determine what value this test brings. We do not add any validation I can see to the options either, so unhappy paths like negative numbers, or urls are not caught at this layer.

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

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

Almost there, thanks @BrunoQuaresma 👍

@BrunoQuaresma BrunoQuaresma merged commit c812453 into main Apr 30, 2024
4 checks passed
@BrunoQuaresma BrunoQuaresma deleted the bq/add-env-test branch April 30, 2024 14:02
Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

This looks fine to me. I think we should port a more rigorous test like this to the serpent package.

Side note, do we not have a linter on this project?

@dannykopping
Copy link
Contributor

Good work @BrunoQuaresma! Thanks for being patient with the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Test to validate that envs given previously will still produce the same options now in Serpent
4 participants