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

test: use t.Setenv to set env vars #59

Merged
merged 1 commit into from
Apr 30, 2023
Merged

test: use t.Setenv to set env vars #59

merged 1 commit into from
Apr 30, 2023

Conversation

Juneezee
Copy link
Contributor

Overview

Starting from Go 1.17, we can use t.Setenv to set environment variable in test. The environment variable is automatically restored to its original value when the test and all its subtests complete. This ensures that each test does not start with leftover environment variables from previous completed tests.

Reference: https://pkg.go.dev/testing#T.Setenv

How to Test

$ go test ./...
?   	sniffer	[no test files]
?   	sniffer/internal/version	[no test files]
?   	sniffer/pkg/context	[no test files]
?   	sniffer/pkg/ebpfev	[no test files]
?   	sniffer/pkg/ebpfev/v1	[no test files]
?   	sniffer/pkg/storageclient	[no test files]
ok  	sniffer/internal/validator	0.014s
ok  	sniffer/pkg/config	0.010s
ok  	sniffer/pkg/config/v1	0.011s
ok  	sniffer/pkg/conthandler	25.045s
ok  	sniffer/pkg/conthandler/v1	0.027s
ok  	sniffer/pkg/ebpfeng	0.015s
ok  	sniffer/pkg/event_data_storage	5.020s
ok  	sniffer/pkg/sbom	0.037s
ok  	sniffer/pkg/sbom/v1	0.043s
ok  	sniffer/pkg/utils	0.013s

This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@dwertent dwertent merged commit 11f5880 into kubescape:main Apr 30, 2023
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.

2 participants