Skip to content

Commit b6b59ed

Browse files
authored
Merge pull request #1062 from kgadams/pathlib
Ditch pathlib and fix Windows bugs
2 parents 5b3d78a + 20eafa1 commit b6b59ed

File tree

31 files changed

+331
-199
lines changed

31 files changed

+331
-199
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

.github/workflows/reusable-testing.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
runs-on: ${{ matrix.os }}
1414
strategy:
1515
matrix:
16-
os: ["macos-latest", "ubuntu-latest"]
16+
os: ["macos-latest", "ubuntu-latest", "windows-latest"]
1717
go_vers: ["1.23", "1.24"]
1818
steps:
1919
- uses: actions/checkout@v2
@@ -30,4 +30,4 @@ jobs:
3030
run: go mod download -x
3131

3232
- name: Test
33-
run: go run github.com/go-task/task/v3/cmd/task test.ci
33+
run: go run github.com/go-task/task/v3/cmd/task test.ci

Taskfile.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,36 @@ tasks:
1616

1717
mocks:
1818
desc: generate new mocks from scratch
19-
deps: [mocks.remove, mocks.generate]
19+
cmds:
20+
- task: mocks.remove
21+
- task: mocks.generate
2022

2123
mocks.remove:
2224
desc: remove all mock files
25+
deps:
26+
- mocks.remove.windows
27+
- mocks.remove.unix
28+
29+
mocks.remove.windows:
30+
desc: remove all mock files on windows
31+
platforms: ["windows"]
32+
cmds:
33+
- rm -f files.txt
34+
- where /r . '*_mock.go' > files.txt || true
35+
- where /r . 'mock_*_test.go' >> files.txt || true
36+
- where /r . 'mocks_moq.go' >> files.txt || true
37+
- where /r . 'mocks_test.go' >> files.txt || true
38+
- where /r . 'mocks_*_test.go' >> files.txt || true
39+
- where /r . 'mocks_matryer_*.go' >> files.txt || true
40+
- where /r . 'mocks.go' >> files.txt || true
41+
- powershell -Command 'foreach ($file in Get-Content "files.txt") { If (Test-Path $file) {Remove-Item $file} }'
42+
- rm -rf mocks/
43+
- rm -f files.txt
44+
45+
46+
mocks.remove.unix:
47+
desc: remove all mock files on unix
48+
platforms: ["darwin", "linux"]
2349
cmds:
2450
- find . -name '*_mock.go' -o -name 'mock_*_test.go' -o -name "mocks_moq.go" -o -name 'mocks_test.go' -o -name 'mocks_*_test.go' -o -name 'mocks_matryer_*.go' -o -name 'mocks.go' | xargs -r rm
2551
- rm -rf mocks/
@@ -77,7 +103,7 @@ tasks:
77103
test.e2e:
78104
desc: run end-to-end tests
79105
cmds:
80-
- ./e2e/run_all.sh
106+
- bash ./e2e/run_all.sh
81107
- go run gotest.tools/gotestsum --format testname -- -v -count=1 ./e2e/...
82108

83109
test.ci:

config/config.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"text/template"
2121

2222
"github.com/brunoga/deep"
23-
"github.com/chigopher/pathlib"
2423
"github.com/go-viper/mapstructure/v2"
2524
koanfYAML "github.com/knadh/koanf/parsers/yaml"
2625
"github.com/knadh/koanf/providers/env"
@@ -93,14 +92,14 @@ type RootConfig struct {
9392
Config `koanf:",squash" yaml:",inline"`
9493
Packages map[string]*PackageConfig `koanf:"packages" yaml:"packages"`
9594
koanf *koanf.Koanf
96-
configFile *pathlib.Path
95+
configFile string
9796
}
9897

9998
func NewRootConfig(
10099
ctx context.Context,
101100
flags *pflag.FlagSet,
102101
) (*RootConfig, *koanf.Koanf, error) {
103-
var configFile *pathlib.Path
102+
var configFile string
104103

105104
log := zerolog.Ctx(ctx)
106105
var err error
@@ -131,24 +130,24 @@ func NewRootConfig(
131130

132131
configFileFromEnv := os.Getenv("MOCKERY_CONFIG")
133132
if configFileFromEnv != "" {
134-
configFile = pathlib.NewPath(configFileFromEnv)
133+
configFile = configFileFromEnv
135134
}
136-
if configFile == nil {
135+
if configFile == "" {
137136
configFileFromFlags, err := flags.GetString("config")
138137
if err != nil {
139138
return nil, nil, fmt.Errorf("getting --config from flags: %w", err)
140139
}
141140
if configFileFromFlags != "" {
142-
configFile = pathlib.NewPath(configFileFromFlags)
141+
configFile = configFileFromFlags
143142
}
144143
}
145-
if configFile == nil {
144+
if configFile == "" {
146145
log.Debug().Msg("config file not specified, searching")
147146
configFile, err = internalConfig.FindConfig()
148147
if err != nil {
149148
return nil, k, fmt.Errorf("discovering mockery config: %w", err)
150149
}
151-
log.Debug().Str("config-file", configFile.String()).Msg("config file found")
150+
log.Debug().Str("config-file", configFile).Msg("config file found")
152151
}
153152
rootConfig.configFile = configFile
154153

@@ -187,7 +186,7 @@ func NewRootConfig(
187186
return nil, nil, stackerr.NewStackErr(err)
188187
}
189188

190-
if err := k.Load(file.Provider(configFile.String()), koanfYAML.Parser()); err != nil {
189+
if err := k.Load(file.Provider(configFile), koanfYAML.Parser()); err != nil {
191190
return nil, k, fmt.Errorf("loading config file: %w", err)
192191
}
193192

@@ -212,7 +211,7 @@ func NewRootConfig(
212211
return &rootConfig, k, nil
213212
}
214213

215-
func (c *RootConfig) ConfigFileUsed() *pathlib.Path {
214+
func (c *RootConfig) ConfigFileUsed() string {
216215
return c.configFile
217216
}
218217

@@ -551,8 +550,8 @@ func (c Config) koanfTagNames() map[string]struct{} {
551550
return tags
552551
}
553552

554-
func (c *Config) FilePath() *pathlib.Path {
555-
return pathlib.NewPath(*c.Dir).Join(*c.FileName).Clean()
553+
func (c *Config) FilePath() string {
554+
return filepath.ToSlash(filepath.Clean(filepath.Join(*c.Dir, *c.FileName)))
556555
}
557556

558557
func (c *Config) ShouldExcludeSubpkg(pkgPath string) bool {
@@ -595,32 +594,35 @@ func (c *Config) ParseTemplates(
595594
if err != nil {
596595
return fmt.Errorf("get working directory: %w", err)
597596
}
598-
interfaceDirPath := pathlib.NewPath(ifaceFilePath).Parent()
599-
interfaceDirRelativePath, err := interfaceDirPath.RelativeToStr(workingDir)
597+
workingDir = filepath.ToSlash(workingDir)
598+
ifaceFilePath = filepath.ToSlash(filepath.Clean(ifaceFilePath))
599+
interfaceDirPath := filepath.ToSlash(filepath.Dir(ifaceFilePath))
600+
interfaceDirRelativePath, err := filepath.Rel(filepath.FromSlash(workingDir), filepath.FromSlash(interfaceDirPath))
600601

601602
var interfaceDirRelative string
602603

603604
if err != nil {
604605
log.Debug().
605606
Err(err).
606607
Str("working-dir", workingDir).
607-
Str("interfaceDirPath", interfaceDirPath.String()).
608-
Str("interface-dir-relative-path", interfaceDirRelativePath.String()).
608+
Str("interfaceDirPath", interfaceDirPath).
609+
Str("interface-dir-relative-path", interfaceDirRelativePath).
609610
Msg("can't make path relative to working dir, setting to './'")
610611
interfaceDirRelative = "."
611612
} else {
613+
interfaceDirRelativePath = filepath.ToSlash(interfaceDirRelativePath)
612614
log.Debug().
613615
Str("working-dir", workingDir).
614-
Str("interfaceDirPath", interfaceDirPath.String()).
615-
Str("interface-dir-relative-path", interfaceDirRelativePath.String()).
616+
Str("interfaceDirPath", interfaceDirPath).
617+
Str("interface-dir-relative-path", interfaceDirRelativePath).
616618
Msg("found relative path")
617-
interfaceDirRelative = interfaceDirRelativePath.String()
619+
interfaceDirRelative = interfaceDirRelativePath
618620
}
619621

620622
// data is the struct sent to the template parser
621623
data := TemplateData{
622624
ConfigDir: filepath.Dir(*c.ConfigFile),
623-
InterfaceDir: interfaceDirPath.String(),
625+
InterfaceDir: interfaceDirPath,
624626
InterfaceDirRelative: interfaceDirRelative,
625627
InterfaceFile: ifaceFilePath,
626628
InterfaceName: ifaceName,

config/config_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
8+
"path"
79
"testing"
810

9-
"github.com/chigopher/pathlib"
1011
"github.com/spf13/pflag"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
@@ -31,13 +32,13 @@ packages:
3132
}
3233
for _, tt := range tests {
3334
t.Run(tt.name, func(t *testing.T) {
34-
configFile := pathlib.NewPath(t.TempDir()).Join("config.yaml")
35-
require.NoError(t, configFile.WriteFile([]byte(tt.config)))
35+
configFile := path.Join(t.TempDir(), "config.yaml")
36+
require.NoError(t, os.WriteFile(configFile, []byte(tt.config), 0o600))
3637

3738
flags := pflag.NewFlagSet("test", pflag.ExitOnError)
3839
flags.String("config", "", "")
3940

40-
require.NoError(t, flags.Parse([]string{"--config", configFile.String()}))
41+
require.NoError(t, flags.Parse([]string{"--config", configFile}))
4142

4243
_, _, err := NewRootConfig(context.Background(), flags)
4344
if tt.wantErr == nil {
@@ -57,16 +58,16 @@ packages:
5758

5859
func TestNewRootConfigUnknownEnvVar(t *testing.T) {
5960
t.Setenv("MOCKERY_UNKNOWN", "foo")
60-
configFile := pathlib.NewPath(t.TempDir()).Join("config.yaml")
61-
require.NoError(t, configFile.WriteFile([]byte(`
61+
configFile := path.Join(t.TempDir(), "config.yaml")
62+
require.NoError(t, os.WriteFile(configFile, []byte(`
6263
packages:
6364
github.com/vektra/mockery/v3:
64-
`)))
65+
`), 0o600))
6566

6667
flags := pflag.NewFlagSet("test", pflag.ExitOnError)
6768
flags.String("config", "", "")
6869

69-
require.NoError(t, flags.Parse([]string{"--config", configFile.String()}))
70+
require.NoError(t, flags.Parse([]string{"--config", configFile}))
7071
_, _, err := NewRootConfig(context.Background(), flags)
7172
assert.NoError(t, err)
7273
}

e2e/test_remote_templates/remote_templates_test.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"fmt"
55
"net/http"
66
"net/http/httptest"
7+
"os"
78
"os/exec"
9+
"path"
810
"testing"
911

10-
"github.com/chigopher/pathlib"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
)
@@ -29,19 +30,15 @@ packages:
2930
`
3031

3132
func TestRemoteTemplates(t *testing.T) {
32-
var err error
33-
3433
// the temp dir needs to reside within the mockery project because mockery
3534
// requires a go.mod file to function correctly. Using t.TempDir() won't work
3635
// because of this.
37-
tmpDirBase := pathlib.NewPath("./test")
38-
_ = tmpDirBase.RemoveAll()
39-
require.NoError(t, tmpDirBase.Mkdir())
40-
tmpDirBase, err = tmpDirBase.ResolveAll()
41-
require.NoError(t, err)
36+
tmpDirBase := "./test"
37+
_ = os.RemoveAll(tmpDirBase)
38+
require.NoError(t, os.Mkdir(tmpDirBase, 0o755))
4239

4340
//nolint:errcheck
44-
defer tmpDirBase.RemoveAll()
41+
defer os.RemoveAll(tmpDirBase)
4542

4643
type test struct {
4744
name string
@@ -92,11 +89,11 @@ func TestRemoteTemplates(t *testing.T) {
9289
},
9390
} {
9491
t.Run(tt.name, func(t *testing.T) {
95-
tmpdir := tmpDirBase.Join(t.Name())
96-
require.NoError(t, tmpdir.MkdirAll())
92+
tmpdir := path.Join(tmpDirBase, t.Name())
93+
require.NoError(t, os.MkdirAll(tmpdir, 0o755))
9794

98-
configFile := tmpdir.Join(".mockery.yml")
99-
outFile := tmpdir.Join("out.txt")
95+
configFile := path.Join(tmpdir, ".mockery.yml")
96+
outFile := path.Join(tmpdir, "out.txt")
10097

10198
templateName := "template.templ"
10299
mux := http.NewServeMux()
@@ -112,23 +109,24 @@ func TestRemoteTemplates(t *testing.T) {
112109

113110
fullPath := fmt.Sprintf("%s/%s", ts.URL, templateName)
114111

112+
parent, name := path.Split(outFile)
115113
configFileContents := fmt.Sprintf(
116114
configTemplate,
117-
outFile.Parent().String(),
118-
outFile.Name(),
115+
parent,
116+
name,
119117
fullPath,
120118
)
121-
require.NoError(t, configFile.WriteFile([]byte(configFileContents)))
119+
require.NoError(t, os.WriteFile(configFile, []byte(configFileContents), 0o600))
122120

123121
//nolint: gosec
124122
out, err := exec.Command(
125123
"go", "run", "github.com/vektra/mockery/v3",
126-
"--config", configFile.String()).CombinedOutput()
124+
"--config", configFile).CombinedOutput()
127125
if tt.expectMockeryErr {
128126
assert.Error(t, err)
129127
} else {
130128
require.NoError(t, err, string(out))
131-
outFileBytes, err := outFile.ReadFile()
129+
outFileBytes, err := os.ReadFile(outFile)
132130
require.NoError(t, err)
133131
assert.Equal(t, "Hello, world!", string(outFileBytes))
134132
}

e2e/test_template_data_schema_validation/validation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package test_template_exercise
22

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

8-
"github.com/chigopher/pathlib"
99
"github.com/stretchr/testify/assert"
1010
)
1111

1212
func TestExercise(t *testing.T) {
1313
t.Parallel()
14-
outfile := pathlib.NewPath("./exercise.txt")
14+
outfile := "./exercise.txt"
1515
//nolint:errcheck
16-
defer outfile.Remove()
16+
defer os.Remove(outfile)
1717

1818
out, err := exec.Command(
1919
"go", "run", "github.com/vektra/mockery/v3",

e2e/test_template_exercise/exercise_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ import (
66
"os/exec"
77
"testing"
88

9-
"github.com/chigopher/pathlib"
109
"github.com/stretchr/testify/assert"
1110
)
1211

1312
func TestExercise(t *testing.T) {
1413
t.Parallel()
1514

16-
outfile := pathlib.NewPath("./exercise.txt")
15+
outfile := "./exercise.txt"
1716
//nolint:errcheck
18-
defer outfile.Remove()
17+
defer os.Remove(outfile)
1918

2019
out, err := exec.Command(
2120
"go", "run", "github.com/vektra/mockery/v3",
@@ -26,13 +25,13 @@ func TestExercise(t *testing.T) {
2625
os.Exit(1)
2726
}
2827

29-
b, err := outfile.ReadFile()
28+
b, err := os.ReadFile(outfile)
3029
if err != nil {
3130
fmt.Println(err)
3231
os.Exit(1)
3332
}
34-
expectedPath := pathlib.NewPath("exercise_expected.txt")
35-
expected, err := expectedPath.ReadFile()
33+
expectedPath := "exercise_expected.txt"
34+
expected, err := os.ReadFile(expectedPath)
3635
if err != nil {
3736
fmt.Println(err)
3837
os.Exit(1)

0 commit comments

Comments
 (0)