Skip to content

Commit d879f88

Browse files
authored
Modernize codebase: Fix linting issues, update CI/CD workflow, and improve test categorization (#549)
* Fix linter failures * Lint in modern Go * Update github workflows * Fix linting * Remove io/ioutil * Modern Go code * Only main * Verbose unit tests * Split tests into integration and unit with short flag * Don't use integration flag * Use t.TempDir instead of custom temp dirs * Remove more manual temp dirs * Fix Hg tests * Remove mercurial stuff * Cleanup hg/mercurial messy test * Upgrade to Go 1.24, remove 1.23 validation * Remove ancient go mod download * Fix caching
1 parent c52d97c commit d879f88

25 files changed

+307
-253
lines changed

.github/workflows/go-getter.yml

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
name: go-getter
22

3-
on: [push]
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
48

59
env:
610
TEST_RESULTS_PATH: /tmp/test-results
711

812
jobs:
913

10-
linux-tests:
14+
# Basic validation that runs on both PRs and pushes (safe operations)
15+
basic-validation:
1116
runs-on: ubuntu-latest
1217
strategy:
1318
matrix:
1419
go-version:
15-
- 1.18
16-
- 1.19
17-
permissions:
18-
id-token: write
19-
contents: read
20+
- "1.24"
21+
- "1.25"
2022
steps:
2123
- name: Setup go
2224
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
@@ -26,23 +28,17 @@ jobs:
2628
- name: Checkout code
2729
uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v4.0.0
2830

29-
- name: Create test directory
30-
run: |
31-
mkdir -p ${{ env.TEST_RESULTS_PATH }}
32-
3331
- name: Setup cache for go modules
3432
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
3533
with:
3634
path: |
3735
~/.cache/go-build
3836
~/go/pkg/mod
39-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
37+
key: ${{ runner.os }}-go${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
4038
restore-keys: |
39+
${{ runner.os }}-go${{ matrix.go-version }}-
4140
${{ runner.os }}-go-
4241
43-
- name: Download go modules
44-
run: go mod download
45-
4642
# Check go fmt output because it does not report non-zero when there are fmt changes
4743
- name: Run gofmt
4844
run: |
@@ -54,6 +50,74 @@ jobs:
5450
exit 1
5551
fi
5652
53+
- name: Build all packages
54+
run: go build ./...
55+
56+
- name: Run unit tests (without cloud integration)
57+
run: go test -short -v ./...
58+
59+
# Linter runs on both PRs and pushes (safe operation)
60+
linter:
61+
runs-on: ubuntu-latest
62+
steps:
63+
- name: Checkout code
64+
uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v4.0.0
65+
66+
- name: Setup go
67+
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
68+
with:
69+
go-version: "1.25" # Use latest for linting
70+
71+
- name: Setup cache for go modules
72+
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
73+
with:
74+
path: |
75+
~/.cache/go-build
76+
~/go/pkg/mod
77+
~/.cache/golangci-lint
78+
key: ${{ runner.os }}-lint-${{ hashFiles('**/go.sum') }}
79+
restore-keys: |
80+
${{ runner.os }}-lint-
81+
82+
- name: Lint code
83+
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
84+
85+
# Full integration tests with cloud resources (only on push to protected branches)
86+
linux-integration-tests:
87+
runs-on: ubuntu-latest
88+
if: github.event_name == 'push'
89+
strategy:
90+
matrix:
91+
go-version:
92+
- "1.24"
93+
- "1.25"
94+
permissions:
95+
id-token: write
96+
contents: read
97+
steps:
98+
- name: Setup go
99+
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # v5.5.0
100+
with:
101+
go-version: ${{ matrix.go-version }}
102+
103+
- name: Checkout code
104+
uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v4.0.0
105+
106+
- name: Create test directory
107+
run: |
108+
mkdir -p ${{ env.TEST_RESULTS_PATH }}
109+
110+
- name: Setup cache for go modules
111+
uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
112+
with:
113+
path: |
114+
~/.cache/go-build
115+
~/go/pkg/mod
116+
key: ${{ runner.os }}-go${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
117+
restore-keys: |
118+
${{ runner.os }}-go${{ matrix.go-version }}-
119+
${{ runner.os }}-go-
120+
57121
- name: Install gotestsum
58122
run: go install gotest.tools/gotestsum@v1.8.2
59123

@@ -86,13 +150,14 @@ jobs:
86150
name: linux-test-results-${{ matrix.go-version }}
87151
path: linux_cov.part
88152

89-
windows-tests:
153+
windows-integration-tests:
90154
runs-on: windows-latest
155+
if: github.event_name == 'push'
91156
strategy:
92157
matrix:
93158
go-version:
94-
- 1.18
95-
- 1.19
159+
- "1.24"
160+
- "1.25"
96161
permissions:
97162
id-token: write
98163
contents: read
@@ -114,13 +179,11 @@ jobs:
114179
path: |
115180
~\AppData\Local\go-build
116181
~\go\pkg\mod
117-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
182+
key: ${{ runner.os }}-go${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
118183
restore-keys: |
184+
${{ runner.os }}-go${{ matrix.go-version }}-
119185
${{ runner.os }}-go-
120186
121-
- name: Download go modules
122-
run: go mod download
123-
124187
- name: Install gotestsum
125188
shell: bash
126189
run: go install gotest.tools/gotestsum@v1.8.2
@@ -154,11 +217,3 @@ jobs:
154217
with:
155218
name: windows-test-results-${{ matrix.go-version }}
156219
path: win_cov.part
157-
158-
linter:
159-
runs-on: ubuntu-latest
160-
steps:
161-
- name: Checkout code
162-
uses: actions/checkout@ff7abcd0c3c05ccf6adc123a8cd1fd4fb30fb493 # v4.0.0
163-
- name: Lint code
164-
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0

client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"errors"
99
"fmt"
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"strconv"
@@ -205,7 +204,7 @@ func (c *Client) Get() error {
205204
if decompressor != nil {
206205
// Create a temporary directory to store our archive. We delete
207206
// this at the end of everything.
208-
td, err := ioutil.TempDir("", "getter")
207+
td, err := os.MkdirTemp("", "getter")
209208
if err != nil {
210209
return fmt.Errorf(
211210
"Error creating temporary directory for archive: %s", err)

client_option_progress_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"net/http"
99
"net/http/httptest"
10-
"os"
1110
"path/filepath"
1211
"sync"
1312
"testing"
@@ -40,17 +39,15 @@ func TestGet_progress(t *testing.T) {
4039
defer s.Close()
4140

4241
{ // dl without tracking
43-
dst := tempTestFile(t)
44-
defer func() { _ = os.RemoveAll(filepath.Dir(dst)) }()
42+
dst := filepath.Join(t.TempDir(), "test-file")
4543
if err := GetFile(dst, s.URL+"/file?thig=this&that"); err != nil {
4644
t.Fatalf("download failed: %v", err)
4745
}
4846
}
4947

5048
{ // tracking
5149
p := &MockProgressTracking{}
52-
dst := tempTestFile(t)
53-
defer func() { _ = os.RemoveAll(filepath.Dir(dst)) }()
50+
dst := filepath.Join(t.TempDir(), "test-file")
5451
if err := GetFile(dst, s.URL+"/file?thig=this&that", WithProgress(p)); err != nil {
5552
t.Fatalf("download failed: %v", err)
5653
}

common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
package getter
55

66
import (
7-
"io/ioutil"
7+
"os"
88
)
99

1010
func tmpFile(dir, pattern string) (string, error) {
11-
f, err := ioutil.TempFile(dir, pattern)
11+
f, err := os.CreateTemp(dir, pattern)
1212
if err != nil {
1313
return "", err
1414
}

decompress.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package getter
55

66
import (
77
"os"
8+
"slices"
89
"strings"
910
)
1011

@@ -72,12 +73,7 @@ func containsDotDot(v string) bool {
7273
if !strings.Contains(v, "..") {
7374
return false
7475
}
75-
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
76-
if ent == ".." {
77-
return true
78-
}
79-
}
80-
return false
76+
return slices.Contains(strings.FieldsFunc(v, isSlashRune), "..")
8177
}
8278

8379
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }

decompress_tar_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package getter
66
import (
77
"archive/tar"
88
"bytes"
9-
"io/ioutil"
109
"os"
1110
"path/filepath"
1211
"runtime"
@@ -82,14 +81,11 @@ func TestTarLimits(t *testing.T) {
8281
t.Fatal(err)
8382
}
8483

85-
td, err := ioutil.TempDir("", "getter")
86-
if err != nil {
87-
t.Fatalf("err: %s", err)
88-
}
84+
td := t.TempDir()
8985

9086
tarFilePath := filepath.Join(td, "input.tar")
9187

92-
err = os.WriteFile(tarFilePath, b.Bytes(), 0666)
88+
err := os.WriteFile(tarFilePath, b.Bytes(), 0666)
9389
if err != nil {
9490
t.Fatalf("err: %s", err)
9591
}
@@ -133,15 +129,12 @@ func TestTarLimits(t *testing.T) {
133129

134130
// testDecompressPermissions decompresses a directory and checks the permissions of the expanded files
135131
func testDecompressorPermissions(t *testing.T, d Decompressor, input string, expected map[string]int, umask os.FileMode) {
136-
td, err := ioutil.TempDir("", "getter")
137-
if err != nil {
138-
t.Fatalf("err: %s", err)
139-
}
132+
td := t.TempDir()
140133

141134
// Destination is always joining result so that we have a new path
142135
dst := filepath.Join(td, "subdir", "result")
143136

144-
err = d.Decompress(dst, input, true, umask)
137+
err := d.Decompress(dst, input, true, umask)
145138
if err != nil {
146139
t.Fatalf("err: %s", err)
147140
}

decompress_testing.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/md5"
88
"encoding/hex"
99
"io"
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"reflect"
@@ -36,7 +35,7 @@ func TestDecompressor(t testing.TB, d Decompressor, cases []TestDecompressCase)
3635
t.Logf("Testing: %s", tc.Input)
3736

3837
// Temporary dir to store stuff
39-
td, err := ioutil.TempDir("", "getter")
38+
td, err := os.MkdirTemp("", "getter")
4039
if err != nil {
4140
t.Fatalf("err: %s", err)
4241
}

decompress_zip_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package getter
66
import (
77
"archive/zip"
88
"bytes"
9-
"io/ioutil"
109
"log"
1110
"os"
1211
"path/filepath"
@@ -169,14 +168,11 @@ func TestDecompressZipBomb(t *testing.T) {
169168
}
170169
}
171170

172-
td, err := ioutil.TempDir("", "go-getter-zip")
173-
if err != nil {
174-
t.Fatalf("err: %s", err)
175-
}
171+
td := t.TempDir()
176172

177173
zipFilePath := filepath.Join(td, "input.zip")
178174

179-
err = os.WriteFile(zipFilePath, buf.Bytes(), 0666)
175+
err := os.WriteFile(zipFilePath, buf.Bytes(), 0666)
180176
if err != nil {
181177
t.Fatalf("err: %s", err)
182178
}

detect_bitbucket_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestBitBucketDetector(t *testing.T) {
3838
f := new(BitBucketDetector)
3939
for i, tc := range cases {
4040
var err error
41-
for i := 0; i < 3; i++ {
41+
for i := range 3 {
4242
var output string
4343
var ok bool
4444
output, ok, err = f.Detect(tc.Input, pwd)

detect_file_unix_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package getter
88

99
import (
10-
"io/ioutil"
1110
"os"
1211
"path/filepath"
1312
"testing"
@@ -16,16 +15,11 @@ import (
1615
// If a relative symlink is passed in as the pwd to Detect, the resulting URL
1716
// can have an invalid path.
1817
func TestFileDetector_relativeSymlink(t *testing.T) {
19-
tmpDir, err := ioutil.TempDir("", "go-getter")
20-
if err != nil {
21-
t.Fatal(err)
22-
}
23-
24-
defer func() { _ = os.RemoveAll(tmpDir) }()
18+
tmpDir := t.TempDir()
2519

2620
// We may have a symlinked tmp dir,
2721
// e.g. OSX uses /var -> /private/var
28-
tmpDir, err = filepath.EvalSymlinks(tmpDir)
22+
tmpDir, err := filepath.EvalSymlinks(tmpDir)
2923
if err != nil {
3024
t.Fatal(err)
3125
}

0 commit comments

Comments
 (0)