Skip to content

Commit 5442c00

Browse files
authored
fix: make it os compatible. (Azure#24040)
* fix: replaced path package methods to filepath methods. path package methods are used for url and unix specific operation. filepath pacakge methods are used for file system operation and cross platform. * fix: replace strings.HasSuffix with filepath.Ext. Use filepath.Ext for file system extension. * fix: replace strings.Contains. - Update findCoverageFile to match exact file names. - Enhance includePreviewPredicate for exact names. - Direct file checking of go.mod. - Removed hardcoded previewSubdir const.
1 parent a4051bf commit 5442c00

File tree

11 files changed

+31
-33
lines changed

11 files changed

+31
-33
lines changed

eng/tools/generator/cmd/issue/link/common.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"log"
11-
"path"
1211
"path/filepath"
1312
"strings"
1413

@@ -45,7 +44,7 @@ func GetReadmePathFromChangedFiles(ctx context.Context, client *query.Client, fi
4544
// find readme files one by one
4645
readmeFiles := make(map[Readme]bool)
4746
for _, file := range files {
48-
readme, err := GetReadmeFromPath(ctx, client, path.Dir(file))
47+
readme, err := GetReadmeFromPath(ctx, client, filepath.Dir(file))
4948
if err != nil {
5049
log.Printf("Changed file '%s' does not belong to any RP, ignoring", file)
5150
continue
@@ -145,7 +144,7 @@ func GetTspConfigPathFromChangedFiles(ctx context.Context, client *query.Client,
145144
// find readme files one by one
146145
tspConfigFiles := make(map[Readme]bool)
147146
for _, file := range files {
148-
tspConfig, err := GetTspConfigFromPath(ctx, client, path.Dir(file))
147+
tspConfig, err := GetTspConfigFromPath(ctx, client, filepath.Dir(file))
149148
if err != nil {
150149
log.Printf("Changed file '%s' does not belong to any RP, ignoring", file)
151150
continue

eng/tools/generator/cmd/v2/common/changelogProcessor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"log"
1212
"net/http"
1313
"os"
14-
"path"
14+
"path/filepath"
1515
"regexp"
1616
"sort"
1717
"strings"
@@ -86,8 +86,8 @@ func ContainsPreviewAPIVersion(packagePath string) (bool, error) {
8686
}
8787

8888
for _, file := range files {
89-
if strings.HasSuffix(file.Name(), ".go") {
90-
b, err := os.ReadFile(path.Join(packagePath, file.Name()))
89+
if filepath.Ext(file.Name()) == ".go" {
90+
b, err := os.ReadFile(filepath.Join(packagePath, file.Name()))
9191
if err != nil {
9292
return false, err
9393
}

eng/tools/generator/cmd/v2/common/fileProcessor.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"io/fs"
1616
"log"
1717
"os"
18-
"path"
1918
"path/filepath"
2019
"regexp"
2120
"slices"
@@ -148,7 +147,7 @@ func CleanSDKGeneratedFiles(path string) error {
148147
return nil
149148
}
150149

151-
if strings.HasSuffix(d.Name(), ".go") {
150+
if filepath.Ext(d.Name()) == ".go" {
152151
b, err := os.ReadFile(path)
153152
if err != nil {
154153
return err
@@ -232,7 +231,7 @@ func RemoveTagSet(path string) error {
232231

233232
// get swagger rp folder name from autorest.md file
234233
func GetSpecRpName(packageRootPath string) (string, error) {
235-
b, err := os.ReadFile(path.Join(packageRootPath, "autorest.md"))
234+
b, err := os.ReadFile(filepath.Join(packageRootPath, "autorest.md"))
236235
if err != nil {
237236
return "", err
238237
}

eng/tools/generator/cmd/v2/refresh/refreshCmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"fmt"
88
"log"
99
"os"
10-
"path"
10+
"path/filepath"
1111
"strings"
1212
"time"
1313

@@ -117,7 +117,7 @@ func (c *commandContext) execute(sdkRepoParam, specRepoParam string) error {
117117

118118
var rpNames []string
119119
if c.flags.RPs == "" {
120-
rps, err := os.ReadDir(path.Join(generateCtx.SDKPath, "sdk", "resourcemanager"))
120+
rps, err := os.ReadDir(filepath.Join(generateCtx.SDKPath, "sdk", "resourcemanager"))
121121
if err != nil {
122122
return fmt.Errorf("failed to get all rps: %+v", err)
123123
}
@@ -129,14 +129,14 @@ func (c *commandContext) execute(sdkRepoParam, specRepoParam string) error {
129129
}
130130

131131
for _, rpName := range rpNames {
132-
namespaces, err := os.ReadDir(path.Join(generateCtx.SDKPath, "sdk", "resourcemanager", rpName))
132+
namespaces, err := os.ReadDir(filepath.Join(generateCtx.SDKPath, "sdk", "resourcemanager", rpName))
133133
if err != nil {
134134
continue
135135
}
136136

137137
for _, namespace := range namespaces {
138138
log.Printf("Release generation for rp: %s, namespace: %s", rpName, namespace.Name())
139-
specRpName, err := common.GetSpecRpName(path.Join(generateCtx.SDKPath, "sdk", "resourcemanager", rpName, namespace.Name()))
139+
specRpName, err := common.GetSpecRpName(filepath.Join(generateCtx.SDKPath, "sdk", "resourcemanager", rpName, namespace.Name()))
140140
if err != nil {
141141
continue
142142
}

eng/tools/generator/cmd/v2/release/releaseCmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"log"
1010
"os"
11-
"path"
11+
"path/filepath"
1212
"strings"
1313
"time"
1414

@@ -148,7 +148,7 @@ func (c *commandContext) execute(sdkRepoParam, specRepoParam string) error {
148148
return err
149149
}
150150

151-
if path.Ext(c.rpName) == ".json" {
151+
if filepath.Ext(c.rpName) == ".json" {
152152
return c.generateFromRequest(sdkRepo, specRepoParam, specCommitHash)
153153
} else {
154154
return c.generate(sdkRepo, specCommitHash)

eng/tools/internal/coverage/coverage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func findCoverageFiles(root string) []string {
4141
if err != nil {
4242
return err
4343
}
44-
if strings.Contains(path, coverageXmlFile) {
44+
if !d.IsDir() && d.Name() == coverageXmlFile {
4545
coverageFiles = append(coverageFiles, path)
4646
}
4747

eng/tools/mgmtreport/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"log"
1313
"net/http"
1414
"os"
15-
"path"
1615
"path/filepath"
1716
"sort"
1817
"strconv"
@@ -209,19 +208,19 @@ func execute(sdkPath, personalAccessToken, storageAccountKey string) error {
209208
}
210209

211210
log.Println("generate a report in Markdown format...")
212-
err = generateMDReport(mgmtReport, path.Join(sdkPath, "mgmtReport.md"))
211+
err = generateMDReport(mgmtReport, filepath.Join(sdkPath, "mgmtReport.md"))
213212
if err != nil {
214213
log.Fatal(err)
215214
}
216215

217216
log.Println("generate a report in HTML format...")
218-
err = generateHTMLReport(mgmtReport, path.Join(sdkPath, "mgmtReport.html"))
217+
err = generateHTMLReport(mgmtReport, filepath.Join(sdkPath, "mgmtReport.html"))
219218
if err != nil {
220219
return err
221220
}
222221

223222
log.Println("upload mgmt report to cloud...")
224-
err = uploadHTMLReport(path.Join(sdkPath, "mgmtReport.html"), storageAccountName, storageAccountKey, storageContainerName, containerBlobName)
223+
err = uploadHTMLReport(filepath.Join(sdkPath, "mgmtReport.html"), storageAccountName, storageAccountKey, storageContainerName, containerBlobName)
225224
if err != nil {
226225
return err
227226
}

eng/tools/profileBuilder/model/latest.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ import (
1111
"io/fs"
1212
"io/ioutil"
1313
"log"
14-
"os"
1514
"path/filepath"
1615
"regexp"
1716
"sort"
1817
"strconv"
1918
"strings"
2019
)
2120

22-
const previewSubdir = string(os.PathSeparator) + "preview" + string(os.PathSeparator)
23-
2421
var previewVer = regexp.MustCompile(`(?:v?\d{4}-\d{2}-\d{2}|v?\d+[\.\d+\.\d\-]*)(?:-preview|-beta)`)
2522

2623
// these predicates are used when walking the package directories.
@@ -31,9 +28,14 @@ func acceptAllPredicate(name string) bool {
3128
}
3229

3330
func includePreviewPredicate(name string) bool {
34-
// check if the path contains a /preview/ subdirectory
35-
if strings.Contains(name, previewSubdir) {
36-
return false
31+
// Split the path into components
32+
components := strings.Split(filepath.ToSlash(name), "/")
33+
34+
// Check if any component is "preview"
35+
for _, component := range components {
36+
if component == "preview" {
37+
return false
38+
}
3739
}
3840
return !previewVer.MatchString(name)
3941
}

eng/tools/profileBuilder/model/transforms.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"log"
2323
"os"
2424
"os/exec"
25-
"path"
2625
"path/filepath"
2726
"strings"
2827
"sync"
@@ -191,7 +190,7 @@ func updateAliasPackageUserAgent(ap *AliasPackage, profileName string) {
191190
func writeAliasPackage(ap *AliasPackage, outputPath string, outputLog, errLog *log.Logger) {
192191
files := token.NewFileSet()
193192

194-
err := os.MkdirAll(path.Dir(outputPath), 0755|os.ModeDir)
193+
err := os.MkdirAll(filepath.Dir(outputPath), 0755|os.ModeDir)
195194
if err != nil {
196195
errLog.Fatalf("error creating directory: %v", err)
197196
}

eng/tools/smoketests/cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func findModuleDirectories(root string) []string {
8484

8585
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
8686
handle(err)
87-
if strings.Contains(d.Name(), "go.mod") && !inIgnoredDirectories(path) {
87+
if d.Name() == "go.mod" && !inIgnoredDirectories(path) {
8888
path = strings.ReplaceAll(path, "\\", "/")
8989
path = strings.ReplaceAll(path, "/go.mod", "")
9090
parts := strings.Split(path, "/sdk/")
@@ -234,7 +234,7 @@ func FindExampleFiles(root, serviceDirectory string) ([]string, error) {
234234

235235
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
236236
handle(err)
237-
if strings.HasPrefix(d.Name(), "example_") && !inIgnoredDirectories(path) && strings.HasSuffix(d.Name(), ".go") {
237+
if strings.HasPrefix(d.Name(), "example_") && !inIgnoredDirectories(path) && filepath.Ext(d.Name()) == ".go" {
238238
path = strings.ReplaceAll(path, "\\", "/")
239239
if serviceDirectory == "" || strings.Contains(path, serviceDirectory) {
240240
ret = append(ret, path)
@@ -308,7 +308,7 @@ func CopyExampleFiles(exFiles []string, dest string) {
308308
func ReplacePackageStatement(root string) error {
309309
packageName := "package main"
310310
return filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
311-
if strings.HasSuffix(d.Name(), ".go") {
311+
if filepath.Ext(d.Name()) == ".go" {
312312
handle(err)
313313
data, err := ioutil.ReadFile(path)
314314
handle(err)
@@ -368,7 +368,7 @@ func FindEnvVars(root string) error {
368368
fmt.Println("Find all environment variables using `os.Getenv` or `os.LookupEnv`")
369369

370370
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
371-
if strings.HasSuffix(path, ".go") {
371+
if filepath.Ext(path) == ".go" {
372372
// Find Env Vars
373373
searchFile(path)
374374
}

0 commit comments

Comments
 (0)