From 558a299a016fe18afc4d9f594a86af41af514e64 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 21 Sep 2021 11:20:25 +0200 Subject: [PATCH] feat: use TB.Setenv on Go 1.17 Signed-off-by: Mark Sagi-Kazar --- internal/testutil/env.go | 9 --------- internal/testutil/env_go1_16.go | 22 ++++++++++++++++++++++ internal/testutil/env_go1_17.go | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 internal/testutil/env_go1_16.go create mode 100644 internal/testutil/env_go1_17.go diff --git a/internal/testutil/env.go b/internal/testutil/env.go index e5eaddd..f2427f5 100644 --- a/internal/testutil/env.go +++ b/internal/testutil/env.go @@ -9,15 +9,6 @@ import ( // Licensed under the MIT license // Copyright (c) 2017 Canonical Ltd. -// Setenv sets an environment variable to a temporary value for the -// duration of the test. -// -// At the end of the test (see "Deferred execution" in the package docs), the -// environment variable is returned to its original value. -func Setenv(t *testing.T, name, val string) { - setenv(t, name, val, true) -} - // Unsetenv unsets an environment variable for the duration of a test. func Unsetenv(t *testing.T, name string) { setenv(t, name, "", false) diff --git a/internal/testutil/env_go1_16.go b/internal/testutil/env_go1_16.go new file mode 100644 index 0000000..5c1f7f9 --- /dev/null +++ b/internal/testutil/env_go1_16.go @@ -0,0 +1,22 @@ +//go:build !go1.17 +// +build !go1.17 + +package testutil + +import ( + "os" + "testing" +) + +// Based on https://github.com/frankban/quicktest/blob/577841610793d24f99e31cc2c0ef3a541fefd7c7/patch.go#L34-L64 +// Licensed under the MIT license +// Copyright (c) 2017 Canonical Ltd. + +// Setenv sets an environment variable to a temporary value for the +// duration of the test. +// +// At the end of the test (see "Deferred execution" in the package docs), the +// environment variable is returned to its original value. +func Setenv(t *testing.T, name, val string) { + setenv(t, name, val, true) +} diff --git a/internal/testutil/env_go1_17.go b/internal/testutil/env_go1_17.go new file mode 100644 index 0000000..c7bf1fe --- /dev/null +++ b/internal/testutil/env_go1_17.go @@ -0,0 +1,18 @@ +//go:build go1.17 +// +build go1.17 + +package testutil + +import ( + "testing" +) + +// Setenv sets an environment variable to a temporary value for the +// duration of the test. +// +// This shim can be removed once support for Go <1.17 is dropped. +func Setenv(t *testing.T, name, val string) { + t.Helper() + + t.Setenv(name, val) +}