Skip to content

Commit bbe651f

Browse files
committed
1. Added support for the MinGW environment when working with file system paths
2. Unified verification that the path in the file system is a file 3. Update dependency versions
1 parent 60498e5 commit bbe651f

12 files changed

+337
-47
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ require (
1919
require (
2020
github.com/go-logr/logr v1.4.2 // indirect
2121
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
22-
github.com/google/go-cmp v0.6.0 // indirect
22+
github.com/google/go-cmp v0.7.0 // indirect
2323
github.com/google/pprof v0.0.0-20250208200701-d0013a598941 // indirect
2424
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2525
go.opentelemetry.io/otel v1.34.0 // indirect
2626
golang.org/x/net v0.35.0 // indirect
2727
golang.org/x/sys v0.30.0 // indirect
2828
golang.org/x/text v0.22.0 // indirect
29-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
29+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250224174004-546df14abb99 // indirect
3030
google.golang.org/grpc v1.70.0 // indirect
3131
google.golang.org/protobuf v1.36.5 // indirect
3232
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek
1515
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
1616
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1717
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
18+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
19+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1820
github.com/google/pprof v0.0.0-20250208200701-d0013a598941 h1:43XjGa6toxLpeksjcxs1jIoIyr+vUfOqY2c6HB4bpoc=
1921
github.com/google/pprof v0.0.0-20250208200701-d0013a598941/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
2022
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
@@ -75,6 +77,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b h1:
7577
google.golang.org/genproto/googleapis/rpc v0.0.0-20250212204824-5a70512c5d8b/go.mod h1:8BS3B93F/U1juMFq9+EDk+qOT5CO1R9IzXxG3PTqiRk=
7678
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 h1:DMTIbak9GhdaSxEjvVzAeNZvyc03I61duqNbnm3SU0M=
7779
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
80+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250224174004-546df14abb99 h1:ZSlhAUqC4r8TPzqLXQ0m3upBNZeF+Y8jQ3c4CR3Ujms=
81+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250224174004-546df14abb99/go.mod h1:LuRYeWDFV6WOn90g357N17oMCaxpgCnbi/44qJvDn2I=
7882
google.golang.org/grpc v1.70.0 h1:pWFv03aZoHzlRKHWicjsZytKAiYCtNS0dHbXnIdq7jQ=
7983
google.golang.org/grpc v1.70.0/go.mod h1:ofIJqVKDXx/JiXrwr2IG4/zwdH9txy3IlF40RmcJSQw=
8084
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=

os/env-path.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import (
44
"context"
55
"fmt"
66
"log/slog"
7-
"os"
87
"slices"
98
"strings"
109
)
1110

11+
// func SplitPath(path string) []string {
12+
// if len(path) == 0 {
13+
// return []string{}
14+
// }
15+
// // os.Getenv()
16+
// }
17+
1218
// CleanPath Removes the sdkm directory from paths
1319
//
1420
//nolint:nonamedreturns // To optimize result logging
@@ -23,28 +29,30 @@ func CleanPath(path string, cleanPaths ...string) (result string) {
2329
}
2430

2531
if len(cleanPaths) == 0 {
26-
result = path
32+
result = FixPath(path)
2733

2834
return result
2935
}
3036

31-
var splitPaths = strings.Split(path, string(os.PathListSeparator))
37+
var splitPaths = SplitPathList(path)
3238

3339
splitPaths = slices.DeleteFunc(
3440
splitPaths, func(s string) bool {
3541
return slices.Contains(cleanPaths, s)
3642
},
3743
)
3844

39-
result = strings.Join(splitPaths, string(os.PathListSeparator))
45+
result = strings.Join(splitPaths, _pathListSeparator)
4046

4147
return result
4248
}
4349

4450
func AddBeforePath(path string, paths ...string) string {
51+
path = FixPath(path)
52+
4553
if len(paths) == 0 {
4654
return path
4755
}
4856

49-
return strings.Join(paths, string(os.PathListSeparator)) + string(os.PathListSeparator) + path
57+
return strings.Join(paths, _pathListSeparator) + _pathListSeparator + path
5058
}

os/env-path_windows_test.go

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,69 @@
33
package os_test
44

55
import (
6-
"runtime"
7-
86
itbasisCoreOs "github.com/itbasis/go-tools-core/os"
97
"github.com/onsi/ginkgo/v2"
108
"github.com/onsi/gomega"
119
)
1210

13-
func checkOnlyWindows() {
14-
if runtime.GOOS == "windows" && itbasisCoreOs.IsMinGW() {
15-
ginkgo.Skip("tests only for Windows, not for MinGW")
16-
}
17-
}
11+
var _ = ginkgo.Describe(
12+
"CleanPath", func() {
13+
defer ginkgo.GinkgoRecover()
14+
15+
ginkgo.DescribeTable(
16+
"only windows", func(path string, cleanPaths []string, wantPath string) {
17+
itbasisCoreOs.SkipIfMinGW()
18+
19+
gomega.Expect(itbasisCoreOs.CleanPath(path, cleanPaths...)).To(gomega.Equal(wantPath))
20+
},
21+
ginkgo.Entry(nil, "a;b;c", []string{}, "a;b;c"),
22+
ginkgo.Entry(nil, "a;b;c", []string{"a"}, "b;c"),
23+
ginkgo.Entry(nil, "a;b;a;c", []string{"a"}, "b;c"),
24+
ginkgo.Entry(nil, "a;b;c", []string{"b"}, "a;c"),
25+
ginkgo.Entry(nil, "a;b;c", []string{"c"}, "a;b"),
26+
ginkgo.Entry(nil, "a;b;c", []string{"a", "c"}, "b"),
27+
)
1828

19-
var _ = ginkgo.DescribeTable(
20-
"CleanPath", func(path string, cleanPaths []string, wantPath string) {
21-
checkOnlyWindows()
29+
ginkgo.DescribeTable(
30+
"only MinGW", func(path string, cleanPaths []string, wantPath string) {
31+
itbasisCoreOs.SkipIfNotMinGW()
2232

23-
gomega.Expect(itbasisCoreOs.CleanPath(path, cleanPaths...)).
24-
To(gomega.Equal(wantPath))
33+
gomega.Expect(itbasisCoreOs.CleanPath(path, cleanPaths...)).To(gomega.Equal(wantPath))
34+
},
35+
ginkgo.Entry(nil, "a;b;c", []string{}, "a:b:c"),
36+
ginkgo.Entry(nil, "a;b;c", []string{"a"}, "b:c"),
37+
ginkgo.Entry(nil, "a;b;a;c", []string{"a"}, "b:c"),
38+
ginkgo.Entry(nil, "a;b;c", []string{"b"}, "a:c"),
39+
ginkgo.Entry(nil, "a;b;c", []string{"c"}, "a:b"),
40+
ginkgo.Entry(nil, "a;b;c", []string{"a", "c"}, "b"),
41+
)
2542
},
26-
ginkgo.Entry(nil, "a;b;c", []string{}, "a;b;c"),
27-
ginkgo.Entry(nil, "a;b;c", []string{"a"}, "b;c"),
28-
ginkgo.Entry(nil, "a;b;a;c", []string{"a"}, "b;c"),
29-
ginkgo.Entry(nil, "a;b;c", []string{"b"}, "a;c"),
30-
ginkgo.Entry(nil, "a;b;c", []string{"c"}, "a;b"),
31-
ginkgo.Entry(nil, "a;b;c", []string{"a", "c"}, "b"),
3243
)
3344

34-
var _ = ginkgo.DescribeTable(
35-
"AddBeforePath", func(path string, addPaths []string, wantPath string) {
36-
checkOnlyWindows()
45+
var _ = ginkgo.Describe(
46+
"AddBeforePath", func() {
47+
defer ginkgo.GinkgoRecover()
48+
49+
ginkgo.DescribeTable(
50+
"only windows", func(path string, addPaths []string, wantPath string) {
51+
itbasisCoreOs.SkipIfMinGW()
52+
53+
gomega.Expect(itbasisCoreOs.AddBeforePath(path, addPaths...)).To(gomega.Equal(wantPath))
54+
},
55+
ginkgo.Entry(nil, "a;b;c", []string{}, "a;b;c"),
56+
ginkgo.Entry(nil, "a;b;c", []string{"d"}, "d;a;b;c"),
57+
ginkgo.Entry(nil, "a;b;c", []string{"d", "e"}, "d;e;a;b;c"),
58+
)
59+
60+
ginkgo.DescribeTable(
61+
"only MinGW", func(path string, addPaths []string, wantPath string) {
62+
itbasisCoreOs.SkipIfNotMinGW()
3763

38-
gomega.Expect(itbasisCoreOs.AddBeforePath(path, addPaths...)).
39-
To(gomega.Equal(wantPath))
64+
gomega.Expect(itbasisCoreOs.AddBeforePath(path, addPaths...)).To(gomega.Equal(wantPath))
65+
},
66+
ginkgo.Entry(nil, "a;b;c", []string{}, "a:b:c"),
67+
ginkgo.Entry(nil, "a;b;c", []string{"d"}, "d:a:b:c"),
68+
ginkgo.Entry(nil, "a;b;c", []string{"d", "e"}, "d:e:a:b:c"),
69+
)
4070
},
41-
ginkgo.Entry(nil, "a;b;c", []string{}, "a;b;c"),
42-
ginkgo.Entry(nil, "a;b;c", []string{"d"}, "d;a;b;c"),
43-
ginkgo.Entry(nil, "a;b;c", []string{"d", "e"}, "d;e;a;b;c"),
4471
)

os/file.go

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

33
import (
4+
"io/fs"
45
"log/slog"
5-
"os"
66

77
itbasisCoreLog "github.com/itbasis/go-tools-core/log"
88
)
99

10-
func BeARegularFile(path string) bool {
11-
fileInfo, err := os.Stat(path)
10+
func BeARegularFile(fsys fs.FS, path string) bool {
11+
fileInfo, err := fs.Stat(fsys, path)
1212
if err != nil {
1313
slog.Debug("fail get file info", itbasisCoreLog.SlogAttrError(err))
1414

os/os_suite_internal_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package os
2+
3+
import (
4+
"testing"
5+
6+
itbasisTestUtils "github.com/itbasis/go-test-utils/v5/ginkgo"
7+
"github.com/onsi/ginkgo/v2"
8+
)
9+
10+
func TestOS(t *testing.T) {
11+
itbasisTestUtils.InitGinkgoSuite(t, "OS Suite")
12+
}
13+
14+
func SkipIfMinGW() {
15+
if IsMinGW() {
16+
ginkgo.Skip("Test not for MinGW environment")
17+
}
18+
}
19+
20+
func SkipIfNotMinGW() {
21+
if !IsMinGW() {
22+
ginkgo.Skip("Test only for MinGW environment")
23+
}
24+
}

os/os_suite_test.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

os/path.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package os
2+
3+
import (
4+
"path/filepath"
5+
"regexp"
6+
"runtime"
7+
"strings"
8+
)
9+
10+
const (
11+
_unixPathSeparator = `/`
12+
_windowsPathSeparator = `\`
13+
14+
_unixPathListSeparator = `:`
15+
_windowsPathListSeparator = `;`
16+
)
17+
18+
var (
19+
_pathSeparator string
20+
_pathListSeparator string
21+
22+
_reDriveReplace = regexp.MustCompile(`\b(\S:\\)`)
23+
)
24+
25+
func init() {
26+
_ = GetPathSeparator()
27+
_ = GetPathListSeparator()
28+
}
29+
30+
func GetPathSeparator() string {
31+
if _pathSeparator != "" {
32+
return _pathSeparator
33+
}
34+
35+
if runtime.GOOS == "windows" && !IsMinGW() {
36+
_pathSeparator = _windowsPathSeparator
37+
} else {
38+
_pathSeparator = _unixPathSeparator
39+
}
40+
41+
return _pathSeparator
42+
}
43+
44+
func GetPathListSeparator() string {
45+
if _pathListSeparator != "" {
46+
return _pathListSeparator
47+
}
48+
49+
if runtime.GOOS == "windows" && !IsMinGW() {
50+
_pathListSeparator = _windowsPathListSeparator
51+
} else {
52+
_pathListSeparator = _unixPathListSeparator
53+
}
54+
55+
return _pathListSeparator
56+
}
57+
58+
func FixPath(value string) string {
59+
if !IsMinGW() {
60+
return value
61+
}
62+
63+
var result = _reDriveReplace.ReplaceAllStringFunc(
64+
value, func(s string) string {
65+
return _unixPathSeparator + strings.ToLower(s[0:1]) + _unixPathSeparator
66+
},
67+
)
68+
result = filepath.ToSlash(result)
69+
70+
return strings.Replace(result, _windowsPathListSeparator, _unixPathListSeparator, -1)
71+
}
72+
73+
func SplitPathList(value string) []string {
74+
return strings.Split(FixPath(value), _pathListSeparator)
75+
}

os/path_nix_internal_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build !windows
2+
3+
package os
4+
5+
import (
6+
"github.com/onsi/ginkgo/v2"
7+
"github.com/onsi/gomega"
8+
)
9+
10+
var _ = ginkgo.Describe(
11+
"GetPathSeparator", func() {
12+
gomega.Expect(GetPathSeparator()).To(gomega.Equal(_unixPathSeparator))
13+
},
14+
)
15+
16+
var _ = ginkgo.Describe(
17+
"GetPathListSeparator", func() {
18+
gomega.Expect(GetPathListSeparator()).To(gomega.Equal(_unixPathListSeparator))
19+
},
20+
)

os/path_nix_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build !windows
2+
3+
package os_test
4+
5+
import (
6+
"github.com/itbasis/go-tools-core/os"
7+
"github.com/onsi/ginkgo/v2"
8+
"github.com/onsi/gomega"
9+
)
10+
11+
var _ = ginkgo.DescribeTable(
12+
"FixPath", func(source, want string) {
13+
defer ginkgo.GinkgoRecover()
14+
15+
gomega.Expect(os.FixPath(source)).To(gomega.Equal(want))
16+
},
17+
ginkgo.Entry(nil, `/usr`, `/usr`),
18+
ginkgo.Entry(nil, `/usr:/home`, `/usr:/home`),
19+
)
20+
21+
var _ = ginkgo.DescribeTable(
22+
"SplitPathList", func(source string, want []string) {
23+
defer ginkgo.GinkgoRecover()
24+
25+
gomega.Expect(os.SplitPathList(source)).To(gomega.ConsistOf(want))
26+
},
27+
ginkgo.Entry(nil, `/usr`, []string{`/usr`}),
28+
ginkgo.Entry(nil, `/usr:/home`, []string{`/usr`, `/home`}),
29+
)

0 commit comments

Comments
 (0)