Skip to content

feat: Add golden files to test cli help output #4897

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 6 commits into from
Nov 4, 2022

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Nov 4, 2022

This PR adds golden files to test cli help output.

Any changes to coder --help or coder server --help now need to be verified via occular output.

Like this:

go test ./cli -run=TestCommandHelp
[...]
            	            	Diff:
            	            	--- Expected
            	            	+++ Actual
            	            	@@ -131,3 +131,3 @@
            	            	                                                    improve our product.
            	            	-                                                   Consumes $CODER_TELEMETRY
            	            	+                                                   Consumes $CODER_TELEMETRY_ENABLE
            	            	       --telemetry-trace                            Whether Opentelemetry traces are sent to
            	            	@@ -170,3 +170,3 @@
            	            	                                                    https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
            	            	-                                                   Consumes $CODER_TRACE
            	            	+                                                   Consumes $CODER_TRACE_ENABLE
            	            	       --trace-honeycomb-api-key string             Enables trace exporting to Honeycomb.io
            	Test:       	TestCommandHelpIsGolden/coder_server_--help
            	Messages:   	golden file mismatch: testdata/coder_server_--help.golden, run "make gen", verify and commit the changes
FAIL
FAIL	github.com/coder/coder/cli	0.159s
FAIL
❯ make gen
❯ git add . -p
diff --git a/cli/testdata/coder_server_--help.golden b/cli/testdata/coder_server_--help.golden
index fb71406a..12031e2c 100644
--- a/cli/testdata/coder_server_--help.golden
+++ b/cli/testdata/coder_server_--help.golden
@@ -129,7 +129,7 @@ Flags:
       --telemetry                                  Whether telemetry is enabled or not. Coder
                                                    collects anonymized usage data to help
                                                    improve our product.
-                                                   Consumes $CODER_TELEMETRY
+                                                   Consumes $CODER_TELEMETRY_ENABLE
       --telemetry-trace                            Whether Opentelemetry traces are sent to
                                                    Coder. Coder collects anonymized
                                                    application tracing to help improve our
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? y
@@ -168,7 +168,7 @@ Flags:
                                                    collected. It exports to a backend
                                                    configured by environment variables. See:
                                                    https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md
-                                                   Consumes $CODER_TRACE
+                                                   Consumes $CODER_TRACE_ENABLE
       --trace-honeycomb-api-key string             Enables trace exporting to Honeycomb.io
                                                    using the provided API Key.
                                                    Consumes $CODER_TRACE_HONEYCOMB_API_KEY
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? y

@mafredri mafredri force-pushed the mafredri/cli-help-golden-files branch from 6db8c38 to 71ad866 Compare November 4, 2022 12:46
@mafredri mafredri marked this pull request as ready for review November 4, 2022 12:48
@mafredri mafredri requested a review from a team November 4, 2022 12:48
@mafredri mafredri force-pushed the mafredri/cli-help-golden-files branch 6 times, most recently from c652c69 to c814fa3 Compare November 4, 2022 13:10
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.

Oh this is an interesting way of doing it. The makefile updates the golden files

@mafredri mafredri force-pushed the mafredri/cli-help-golden-files branch from c814fa3 to 7f6b0f4 Compare November 4, 2022 13:30

//nolint:tparallel,paralleltest // These test sets env vars.
func TestCommandHelp(t *testing.T) {
t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

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

You use t.Setenv in the subtests. Can't we not set envs during parallel tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, t.Setenv will panic after a call to t.Parallel(). It modifies the os env but restores it at the end of the test.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, apparently it does not panic if you use t.Setenv in a subtest and you have t.Parallel on the root test.

TestCommandHelp sets t.Parallel on the first line, but subtests use t.Setenv. Do we need to remove t.Parallel from the root test?

Copy link
Member Author

Choose a reason for hiding this comment

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

Huh?

This is from the Go implementation:

func (t *T) Setenv(key, value string) {
	if t.isParallel {
		panic("testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests")
	}

So if the subtest is parallel, it'll panic, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I think I misunderstood. It’s fine if the parent test has t.Parallel. Just means that starting it will not block other tests, but the subtests will.

Copy link
Member

Choose a reason for hiding this comment

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

👍

@mafredri mafredri requested a review from deansheather November 4, 2022 14:00
@mafredri mafredri force-pushed the mafredri/cli-help-golden-files branch from 3ec3ae3 to 1b4dbe9 Compare November 4, 2022 14:03
@mafredri mafredri self-assigned this Nov 4, 2022
Get Started:
- Start a Coder server:

$ coder server
Copy link
Member

Choose a reason for hiding this comment

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

nit: you may consider disabling colors for readability purposes. I assume that the major goal of this change request is to check the CLI text output.

Copy link
Member Author

Choose a reason for hiding this comment

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

They should already be since stdout is not a tty, so seems like a separate issue 😅. (You're absolutely right that it shouldn't be output since we only care about the text.)

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
// Unset all CODER_ environment variables for a clean slate.
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if you shouldn't remove all environment variables, like TTY-related, NO_COLOR, etc.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was being cautious by only overriding the CODER ones, but I'm guessing it won't hurt 👍🏻. Currently (to my knowledge) the only non-coder env that has an effect on the application is CACHE_DIRECTORY (for systemd compat).

Copy link
Member Author

@mafredri mafredri Nov 4, 2022

Choose a reason for hiding this comment

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

Dang, looks like it broke Windows tests, I'll just revert the change and we can fix any issues if they surface in the future. https://github.com/coder/coder/actions/runs/3394713015/jobs/5643623340

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. Maybe there is a minimal set of env vars that are required by Windows.

@mafredri mafredri merged commit 587924f into main Nov 4, 2022
@mafredri mafredri deleted the mafredri/cli-help-golden-files branch November 4, 2022 17:48
@github-actions github-actions bot locked and limited conversation to collaborators Nov 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants