• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

numtide / treefmt / 13808938813

12 Mar 2025 10:18AM UTC coverage: 34.379% (-16.0%) from 50.399%
13808938813

push

github

web-flow
Merge pull request #543 from numtide/feat/go-1.24

feat: update to go 1.24

0 of 2 new or added lines in 1 file covered. (0.0%)

260 existing lines in 5 files now uncovered.

559 of 1626 relevant lines covered (34.38%)

13.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/test/test.go
1
package test
2

3
import (
4
        "fmt"
5
        "os"
6
        "path/filepath"
7
        "testing"
8
        "time"
9

10
        "github.com/BurntSushi/toml"
11
        "github.com/numtide/treefmt/v2/config"
12
        cp "github.com/otiai10/copy"
13
        "github.com/stretchr/testify/require"
14
        "golang.org/x/sys/unix"
15
)
16

UNCOV
17
func WriteConfig(t *testing.T, path string, cfg *config.Config) {
×
UNCOV
18
        t.Helper()
×
UNCOV
19

×
UNCOV
20
        f, err := os.Create(path)
×
UNCOV
21
        if err != nil {
×
22
                t.Fatalf("failed to create a new config file: %v", err)
×
23
        }
×
24

UNCOV
25
        encoder := toml.NewEncoder(f)
×
UNCOV
26
        if err = encoder.Encode(cfg); err != nil {
×
27
                t.Fatalf("failed to write to config file: %v", err)
×
28
        }
×
29
}
30

UNCOV
31
func TempExamples(t *testing.T) string {
×
UNCOV
32
        t.Helper()
×
UNCOV
33
        tempDir := t.TempDir()
×
UNCOV
34
        TempExamplesInDir(t, tempDir)
×
UNCOV
35

×
UNCOV
36
        return tempDir
×
UNCOV
37
}
×
38

UNCOV
39
func TempExamplesInDir(t *testing.T, dir string) {
×
UNCOV
40
        t.Helper()
×
UNCOV
41
        require.NoError(t, cp.Copy("../test/examples", dir), "failed to copy test data to dir")
×
UNCOV
42

×
UNCOV
43
        // we have second precision mod time tracking, so we wait a second before returning, so we don't trigger false
×
UNCOV
44
        // positives for things like fail on change
×
UNCOV
45
        time.Sleep(time.Second)
×
UNCOV
46
}
×
47

UNCOV
48
func TempFile(t *testing.T, dir string, pattern string, contents *string) *os.File {
×
UNCOV
49
        t.Helper()
×
UNCOV
50

×
UNCOV
51
        file, err := os.CreateTemp(dir, pattern)
×
UNCOV
52
        require.NoError(t, err, "failed to create temp file")
×
UNCOV
53

×
UNCOV
54
        if contents == nil {
×
UNCOV
55
                return file
×
UNCOV
56
        }
×
57

UNCOV
58
        _, err = file.WriteString(*contents)
×
UNCOV
59
        require.NoError(t, err, "failed to write contents to temp file")
×
UNCOV
60
        require.NoError(t, file.Close(), "failed to close temp file")
×
UNCOV
61

×
UNCOV
62
        file, err = os.Open(file.Name())
×
UNCOV
63
        require.NoError(t, err, "failed to open temp file")
×
UNCOV
64

×
UNCOV
65
        return file
×
66
}
67

68
// Lutimes is a convenience wrapper for using unix.Lutimes
69
// TODO: this will need to be adapted if we support Windows.
UNCOV
70
func Lutimes(t *testing.T, path string, atime time.Time, mtime time.Time) error {
×
UNCOV
71
        t.Helper()
×
UNCOV
72

×
UNCOV
73
        var utimes [2]unix.Timeval
×
UNCOV
74
        utimes[0] = unix.NsecToTimeval(atime.UnixNano())
×
UNCOV
75
        utimes[1] = unix.NsecToTimeval(mtime.UnixNano())
×
UNCOV
76

×
UNCOV
77
        // Change the timestamps of the path. If it's a symlink, it updates the symlink's timestamps, not the target's.
×
UNCOV
78
        err := unix.Lutimes(path, utimes[0:])
×
UNCOV
79
        if err != nil {
×
80
                return fmt.Errorf("failed to change times: %w", err)
×
81
        }
×
82

UNCOV
83
        return nil
×
84
}
85

UNCOV
86
func LutimesBump(t *testing.T, path string, atime time.Duration, mtime time.Duration) {
×
UNCOV
87
        t.Helper()
×
UNCOV
88

×
UNCOV
89
        now := time.Now()
×
UNCOV
90
        newAtime := now.Add(atime)
×
UNCOV
91
        newMtime := now.Add(mtime)
×
UNCOV
92

×
UNCOV
93
        err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
×
UNCOV
94
                if err != nil || info.IsDir() {
×
UNCOV
95
                        return err
×
UNCOV
96
                }
×
97

UNCOV
98
                return Lutimes(t, path, newAtime, newMtime)
×
99
        })
UNCOV
100
        if err != nil {
×
101
                t.Fatalf("failed to bump modtimes: %v", err)
×
102
        }
×
103
}
104

105
// ChangeWorkDir changes the current working directory for the duration of the test.
106
// The original directory is restored when the test ends.
UNCOV
107
func ChangeWorkDir(t *testing.T, dir string) {
×
UNCOV
108
        t.Helper()
×
UNCOV
109

×
UNCOV
110
        // capture current cwd, so we can replace it after the test is finished
×
UNCOV
111
        cwd, err := os.Getwd()
×
UNCOV
112
        if err != nil {
×
113
                t.Fatal(fmt.Errorf("failed to get current working directory: %w", err))
×
114
        }
×
115

UNCOV
116
        t.Cleanup(func() {
×
UNCOV
117
                // return to the previous working directory
×
NEW
118
                t.Chdir(cwd)
×
119
        })
×
120

121
        // change to the new directory
NEW
122
        t.Chdir(dir)
×
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc