Skip to content

Commit 5d3ece5

Browse files
authored
deprecate git, version-checker, and slack packages (#1838)
* deprecate git, version-checker, and slack packages Per the Terratest V2 proposal, these packages are scheduled for removal in v2: git (each helper wraps a single git command), version-checker (marginal value over shelling out), and slack (out of scope for an IaC testing library). - Add // Deprecated: notices to every exported symbol in all three packages. - git has in-repo callers, so migrate them off it in the same change to avoid staticcheck SA1019 in CI: test-structure and the docker/opa tests now shell out to git directly with os/exec. version-checker and slack have no in-repo callers, so those are annotation-only. No behavior change; the packages keep working for v1. * Show stdlib replacements in deprecation comments Address review: each // Deprecated: comment on git/version-checker/slack now demos the concrete replacement (the exact git command via os/exec, the version regexp + go-version check, the slack-go client call) so users can pattern-match from the IDE hover. Also drop the dead 'migration notes' links in favor of stating there's no public replacement. * git: correct the detached-HEAD note on the rev-parse demo git rev-parse --abbrev-ref HEAD prints "HEAD" (not empty) when detached; the real code maps it to "". Fix the demo so a user copying it doesn't get the wrong result. (Two-agent review.) * Fix wsl lint failures in the git caller migrations Add the blank lines golangci-lint's wsl_v5 rule requires before the if and the assignments in the inlined git replacements (docker, opa, test-structure). Verified with golangci-lint 2.11.3: 0 issues.
1 parent 1adfee6 commit 5d3ece5

8 files changed

Lines changed: 164 additions & 40 deletions

File tree

modules/docker/build_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package docker_test
22

33
import (
4+
"os/exec"
45
"strings"
56
"testing"
67

78
"github.com/gruntwork-io/terratest/modules/docker"
8-
"github.com/gruntwork-io/terratest/modules/git"
99
"github.com/gruntwork-io/terratest/modules/logger"
1010
"github.com/gruntwork-io/terratest/modules/random"
1111
"github.com/stretchr/testify/require"
@@ -102,9 +102,15 @@ func TestGitCloneAndBuild(t *testing.T) {
102102

103103
ctx := t.Context()
104104

105-
gitBranchName := git.GetCurrentBranchNameContext(t, ctx, "")
106-
if gitBranchName == "" {
107-
logger.Default.Logf(t, "WARNING: git.GetCurrentBranchNameContext returned an empty string; falling back to main")
105+
branchOut, err := exec.CommandContext(ctx, "git", "branch", "--show-current").Output()
106+
gitBranchName := ""
107+
108+
if err == nil {
109+
gitBranchName = strings.TrimSpace(string(branchOut))
110+
}
111+
112+
if gitBranchName == "" || gitBranchName == "HEAD" {
113+
logger.Default.Logf(t, "WARNING: could not determine the current git branch; falling back to main")
108114

109115
gitBranchName = "main"
110116
}

modules/git/git.go

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
// Package git allows to interact with Git.
2+
//
3+
// Deprecated: The git package is scheduled for removal in Terratest v2. Each
4+
// helper here wraps a single git command (for example, git rev-parse or
5+
// git describe); call git directly with os/exec instead. There is no public
6+
// replacement; the package is being dropped.
27
package git
38

49
import (
@@ -14,9 +19,8 @@ import (
1419
// GetCurrentBranchName retrieves the current branch name or an empty string
1520
// in case of detached state. Fails the test if an error occurs.
1621
//
17-
// Deprecated: Use [GetCurrentBranchNameContext] instead, which supports context
18-
// cancellation and accepts an explicit working directory rather than relying on
19-
// the process working directory.
22+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
23+
// exec.Command("git", "branch", "--show-current").Output() (empty when detached).
2024
func GetCurrentBranchName(t testing.TestingT) string {
2125
return GetCurrentBranchNameContext(t, context.Background(), "")
2226
}
@@ -25,6 +29,9 @@ func GetCurrentBranchName(t testing.TestingT) string {
2529
// string in case of detached state. The dir parameter specifies the working
2630
// directory for the git command; if empty, the process working directory is
2731
// used. Fails the test if an error occurs.
32+
//
33+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
34+
// exec.CommandContext(ctx, "git", "branch", "--show-current") with cmd.Dir = dir, then .Output().
2835
func GetCurrentBranchNameContext(t testing.TestingT, ctx context.Context, dir string) string {
2936
out, err := GetCurrentBranchNameContextE(t, ctx, dir)
3037
if err != nil {
@@ -38,9 +45,8 @@ func GetCurrentBranchNameContext(t testing.TestingT, ctx context.Context, dir st
3845
// in case of detached state. Uses git branch --show-current, which was
3946
// introduced in git v2.22. Falls back to git rev-parse for older versions.
4047
//
41-
// Deprecated: Use [GetCurrentBranchNameContextE] instead, which supports
42-
// context cancellation and accepts an explicit working directory rather than
43-
// relying on the process working directory.
48+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
49+
// exec.Command("git", "branch", "--show-current").Output() (empty when detached).
4450
func GetCurrentBranchNameE(t testing.TestingT) (string, error) {
4551
return GetCurrentBranchNameContextE(t, context.Background(), "")
4652
}
@@ -50,6 +56,9 @@ func GetCurrentBranchNameE(t testing.TestingT) (string, error) {
5056
// introduced in git v2.22. Falls back to git rev-parse for older versions.
5157
// The dir parameter specifies the working directory for the git command; if
5258
// empty, the process working directory is used.
59+
//
60+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
61+
// exec.CommandContext(ctx, "git", "branch", "--show-current") with cmd.Dir = dir, then .Output().
5362
func GetCurrentBranchNameContextE(t testing.TestingT, ctx context.Context, dir string) (string, error) {
5463
cmd := exec.CommandContext(ctx, "git", "branch", "--show-current")
5564
cmd.Dir = dir
@@ -70,9 +79,8 @@ func GetCurrentBranchNameContextE(t testing.TestingT, ctx context.Context, dir s
7079
// GetCurrentBranchNameOldE retrieves the current branch name or an empty
7180
// string in case of detached state using git rev-parse --abbrev-ref HEAD.
7281
//
73-
// Deprecated: Use [GetCurrentBranchNameOldContextE] instead, which supports
74-
// context cancellation and accepts an explicit working directory rather than
75-
// relying on the process working directory.
82+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
83+
// exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output() (prints "HEAD" when detached; map it to "").
7684
func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) {
7785
return GetCurrentBranchNameOldContextE(t, context.Background(), "")
7886
}
@@ -82,6 +90,9 @@ func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) {
8290
// This is a fallback for git versions older than v2.22 that lack
8391
// git branch --show-current. The dir parameter specifies the working directory
8492
// for the git command; if empty, the process working directory is used.
93+
//
94+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
95+
// exec.CommandContext(ctx, "git", "rev-parse", "--abbrev-ref", "HEAD") with cmd.Dir = dir, then .Output().
8596
func GetCurrentBranchNameOldContextE(t testing.TestingT, ctx context.Context, dir string) (string, error) {
8697
cmd := exec.CommandContext(ctx, "git", "rev-parse", "--abbrev-ref", "HEAD")
8798
cmd.Dir = dir
@@ -103,9 +114,8 @@ func GetCurrentBranchNameOldContextE(t testing.TestingT, ctx context.Context, di
103114
// (non-annotated) tag, or exact tag value if the tag points to the current
104115
// commit. Fails the test if an error occurs.
105116
//
106-
// Deprecated: Use [GetCurrentGitRefContext] instead, which supports context
107-
// cancellation and accepts an explicit working directory rather than relying on
108-
// the process working directory.
117+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly: try
118+
// git branch --show-current, then git describe --tags when detached.
109119
func GetCurrentGitRef(t testing.TestingT) string {
110120
return GetCurrentGitRefContext(t, context.Background(), "")
111121
}
@@ -115,6 +125,9 @@ func GetCurrentGitRef(t testing.TestingT) string {
115125
// commit. The dir parameter specifies the working directory for the git
116126
// command; if empty, the process working directory is used. Fails the test if
117127
// an error occurs.
128+
//
129+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly: try
130+
// git branch --show-current, then git describe --tags when detached (set cmd.Dir = dir).
118131
func GetCurrentGitRefContext(t testing.TestingT, ctx context.Context, dir string) string {
119132
out, err := GetCurrentGitRefContextE(t, ctx, dir)
120133
if err != nil {
@@ -128,9 +141,8 @@ func GetCurrentGitRefContext(t testing.TestingT, ctx context.Context, dir string
128141
// (non-annotated) tag, or exact tag value if the tag points to the current
129142
// commit.
130143
//
131-
// Deprecated: Use [GetCurrentGitRefContextE] instead, which supports context
132-
// cancellation and accepts an explicit working directory rather than relying on
133-
// the process working directory.
144+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly: try
145+
// git branch --show-current, then git describe --tags when detached.
134146
func GetCurrentGitRefE(t testing.TestingT) (string, error) {
135147
return GetCurrentGitRefContextE(t, context.Background(), "")
136148
}
@@ -139,6 +151,9 @@ func GetCurrentGitRefE(t testing.TestingT) (string, error) {
139151
// (non-annotated) tag, or exact tag value if the tag points to the current
140152
// commit. The dir parameter specifies the working directory for the git
141153
// command; if empty, the process working directory is used.
154+
//
155+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly: try
156+
// git branch --show-current, then git describe --tags when detached (set cmd.Dir = dir).
142157
func GetCurrentGitRefContextE(t testing.TestingT, ctx context.Context, dir string) (string, error) {
143158
out, err := GetCurrentBranchNameContextE(t, ctx, dir)
144159
if err != nil {
@@ -160,9 +175,8 @@ func GetCurrentGitRefContextE(t testing.TestingT, ctx context.Context, dir strin
160175
// GetTagE retrieves the lightweight (non-annotated) tag or exact tag value if
161176
// the tag points to the current commit.
162177
//
163-
// Deprecated: Use [GetTagContextE] instead, which supports context
164-
// cancellation and accepts an explicit working directory rather than relying on
165-
// the process working directory.
178+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
179+
// exec.Command("git", "describe", "--tags").Output().
166180
func GetTagE(t testing.TestingT) (string, error) {
167181
return GetTagContextE(t, context.Background(), "")
168182
}
@@ -171,6 +185,9 @@ func GetTagE(t testing.TestingT) (string, error) {
171185
// value if the tag points to the current commit. The dir parameter specifies
172186
// the working directory for the git command; if empty, the process working
173187
// directory is used.
188+
//
189+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
190+
// exec.CommandContext(ctx, "git", "describe", "--tags") with cmd.Dir = dir, then .Output().
174191
func GetTagContextE(t testing.TestingT, ctx context.Context, dir string) (string, error) {
175192
cmd := exec.CommandContext(ctx, "git", "describe", "--tags")
176193
cmd.Dir = dir
@@ -186,16 +203,18 @@ func GetTagContextE(t testing.TestingT, ctx context.Context, dir string) (string
186203
// GetRepoRoot retrieves the path to the root directory of the repo. Fails the
187204
// test if there is an error.
188205
//
189-
// Deprecated: Use [GetRepoRootContext] instead, which supports context
190-
// cancellation and accepts an explicit working directory rather than relying on
191-
// the process working directory.
206+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
207+
// exec.Command("git", "rev-parse", "--show-toplevel").Output().
192208
func GetRepoRoot(t testing.TestingT) string {
193209
return GetRepoRootContext(t, context.Background(), "")
194210
}
195211

196212
// GetRepoRootContext retrieves the path to the root directory of the repo. The
197213
// dir parameter specifies the working directory for the git command; if empty,
198214
// the process working directory is used. Fails the test if there is an error.
215+
//
216+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
217+
// exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") with cmd.Dir = dir, then .Output().
199218
func GetRepoRootContext(t testing.TestingT, ctx context.Context, dir string) string {
200219
out, err := GetRepoRootContextE(t, ctx, dir)
201220
require.NoError(t, err)
@@ -205,16 +224,18 @@ func GetRepoRootContext(t testing.TestingT, ctx context.Context, dir string) str
205224

206225
// GetRepoRootE retrieves the path to the root directory of the repo.
207226
//
208-
// Deprecated: Use [GetRepoRootContextE] instead, which supports context
209-
// cancellation and accepts an explicit working directory rather than relying on
210-
// the process working directory.
227+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
228+
// exec.Command("git", "rev-parse", "--show-toplevel").Output().
211229
func GetRepoRootE(t testing.TestingT) (string, error) {
212230
return GetRepoRootContextE(t, context.Background(), "")
213231
}
214232

215233
// GetRepoRootContextE retrieves the path to the root directory of the repo.
216234
// The dir parameter specifies the working directory for the git command; if
217235
// empty, the process working directory is used.
236+
//
237+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
238+
// exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") with cmd.Dir = dir, then .Output().
218239
func GetRepoRootContextE(t testing.TestingT, ctx context.Context, dir string) (string, error) {
219240
if dir == "" {
220241
cwd, err := os.Getwd()
@@ -231,14 +252,17 @@ func GetRepoRootContextE(t testing.TestingT, ctx context.Context, dir string) (s
231252
// GetRepoRootForDir retrieves the path to the root directory of the repo in
232253
// which dir resides. Fails the test if there is an error.
233254
//
234-
// Deprecated: Use [GetRepoRootForDirContext] instead, which supports context
235-
// cancellation.
255+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
256+
// exec.Command("git", "rev-parse", "--show-toplevel") with cmd.Dir = dir, then .Output().
236257
func GetRepoRootForDir(t testing.TestingT, dir string) string {
237258
return GetRepoRootForDirContext(t, context.Background(), dir)
238259
}
239260

240261
// GetRepoRootForDirContext retrieves the path to the root directory of the
241262
// repo in which dir resides. Fails the test if there is an error.
263+
//
264+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
265+
// exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") with cmd.Dir = dir, then .Output().
242266
func GetRepoRootForDirContext(t testing.TestingT, ctx context.Context, dir string) string {
243267
out, err := GetRepoRootForDirContextE(t, ctx, dir)
244268
require.NoError(t, err)
@@ -249,14 +273,17 @@ func GetRepoRootForDirContext(t testing.TestingT, ctx context.Context, dir strin
249273
// GetRepoRootForDirE retrieves the path to the root directory of the repo in
250274
// which dir resides.
251275
//
252-
// Deprecated: Use [GetRepoRootForDirContextE] instead, which supports context
253-
// cancellation.
276+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
277+
// exec.Command("git", "rev-parse", "--show-toplevel") with cmd.Dir = dir, then .Output().
254278
func GetRepoRootForDirE(t testing.TestingT, dir string) (string, error) {
255279
return GetRepoRootForDirContextE(t, context.Background(), dir)
256280
}
257281

258282
// GetRepoRootForDirContextE retrieves the path to the root directory of the
259283
// repo in which dir resides.
284+
//
285+
// Deprecated: scheduled for removal in Terratest v2. Shell out to git directly, e.g.
286+
// exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") with cmd.Dir = dir, then .Output().
260287
func GetRepoRootForDirContextE(t testing.TestingT, ctx context.Context, dir string) (string, error) {
261288
cmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
262289
cmd.Dir = dir

modules/opa/download_policy_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package opa_test
22

33
import (
44
"os"
5+
"os/exec"
56
"path/filepath"
67
"strings"
78
"sync"
@@ -11,7 +12,6 @@ import (
1112
"github.com/stretchr/testify/require"
1213

1314
"github.com/gruntwork-io/terratest/modules/files"
14-
"github.com/gruntwork-io/terratest/modules/git"
1515
"github.com/gruntwork-io/terratest/modules/opa"
1616
)
1717

@@ -30,7 +30,19 @@ func TestDownloadPolicyReturnsLocalPath(t *testing.T) {
3030
func TestDownloadPolicyDownloadsRemote(t *testing.T) {
3131
t.Parallel()
3232

33-
curRef := git.GetCurrentGitRefContext(t, t.Context(), "")
33+
ctx := t.Context()
34+
35+
branchOut, err := exec.CommandContext(ctx, "git", "branch", "--show-current").Output()
36+
require.NoError(t, err)
37+
38+
curRef := strings.TrimSpace(string(branchOut))
39+
if curRef == "" || curRef == "HEAD" {
40+
tagOut, err := exec.CommandContext(ctx, "git", "describe", "--tags").Output()
41+
require.NoError(t, err)
42+
43+
curRef = strings.TrimSpace(string(tagOut))
44+
}
45+
3446
baseDir := "git::https://github.com/gruntwork-io/terratest.git?ref=" + curRef
3547
localPath := "../../examples/terraform-opa-example/policy/enforce_source.rego"
3648
remotePath := "git::https://github.com/gruntwork-io/terratest.git//examples/terraform-opa-example/policy/enforce_source.rego?ref=" + curRef

modules/slack/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
// Package slack contains routines useful for testing slack integrations.
2+
//
3+
// Deprecated: The slack package is scheduled for removal in Terratest v2. Slack
4+
// validation is out of scope for an infrastructure testing library; use the
5+
// github.com/slack-go/slack client directly if you need it. There is no public
6+
// replacement; the package is being dropped.
27
package slack

modules/slack/validate.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import (
1818
// - Header block text
1919
// All other blocks are ignored in the validation.
2020
// NOTE: This only looks for bot posted messages.
21+
//
22+
// Deprecated: scheduled for removal in Terratest v2. Use the slack-go client directly, e.g.:
23+
//
24+
// client := slack.New(token)
25+
// resp, err := client.GetConversationHistory(&slack.GetConversationHistoryParameters{
26+
// ChannelID: channelID,
27+
// Limit: historyLimit,
28+
// })
29+
// // then scan resp.Messages for the expected text.
2130
func ValidateExpectedSlackMessageE(
2231
t testing.TestingT,
2332
token,
@@ -96,6 +105,8 @@ func checkMessageContainsText(msg *slack.Msg, expectedText string) bool {
96105
}
97106

98107
// MessageNotFoundErr is returned when the expected text cannot be found in any of the messages posted in a Slack channel.
108+
//
109+
// Deprecated: scheduled for removal in Terratest v2 along with the slack package.
99110
type MessageNotFoundErr struct{}
100111

101112
func (err MessageNotFoundErr) Error() string {

modules/test-structure/test_structure.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import (
1414
"context"
1515
"fmt"
1616
"os"
17+
"os/exec"
1718
"path/filepath"
1819
"strings"
1920

20-
"github.com/gruntwork-io/terratest/modules/git"
21-
2221
go_test "testing"
2322

2423
"github.com/gruntwork-io/terratest/modules/files"
@@ -253,10 +252,14 @@ func runValidateOnAllTerraformModulesContext(
253252
) {
254253
t.Helper()
255254

256-
// Find the Git root
257-
gitRoot, err := git.GetRepoRootForDirContextE(t, ctx, opts.RootDir)
255+
// Find the Git root by shelling out to git rev-parse from the target directory.
256+
gitRootCmd := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel")
257+
gitRootCmd.Dir = opts.RootDir
258+
gitRootOut, err := gitRootCmd.Output()
258259
require.NoError(t, err)
259260

261+
gitRoot := strings.TrimSpace(string(gitRootOut))
262+
260263
// Find the relative path between the root dir and the git root
261264
relPath, err := filepath.Rel(gitRoot, opts.RootDir)
262265
require.NoError(t, err)

0 commit comments

Comments
 (0)