Skip to content

Commit f890a37

Browse files
fix(sidekick): Tests and commands run in Windows (#1954)
1 parent 70b68d4 commit f890a37

File tree

7 files changed

+32
-21
lines changed

7 files changed

+32
-21
lines changed

internal/sidekick/downloads_cache.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,30 @@ func downloadAttempt(target, source, expectedSha256 string) error {
119119
if err != nil {
120120
return err
121121
}
122-
defer os.Remove(tempFile.Name())
122+
defer func() {
123+
tempFile.Close()
124+
os.Remove(tempFile.Name())
125+
}()
126+
127+
hasher := sha256.New()
128+
writer := io.MultiWriter(tempFile, hasher)
123129

124130
response, err := http.Get(source)
125131
if err != nil {
126132
return err
127133
}
134+
defer response.Body.Close()
128135
if response.StatusCode >= 300 {
129136
return fmt.Errorf("http error in download %s", response.Status)
130137
}
131138

132-
if _, err := io.Copy(tempFile, response.Body); err != nil {
139+
if _, err := io.Copy(writer, response.Body); err != nil {
133140
return err
134141
}
135142
if err := tempFile.Close(); err != nil {
136143
return err
137144
}
138-
if err := response.Body.Close(); err != nil {
139-
return err
140-
}
141-
file, err := os.Open(tempFile.Name())
142-
if err != nil {
143-
return err
144-
}
145-
hasher := sha256.New()
146-
if _, err := io.Copy(hasher, file); err != nil {
147-
return err
148-
}
145+
149146
got := fmt.Sprintf("%x", hasher.Sum(nil))
150147
if expectedSha256 != got {
151148
return fmt.Errorf("mismatched hash on download, expected=%s, got=%s", expectedSha256, got)

internal/sidekick/internal/dart/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Generate(model *api.API, outdir string, config *config.Config) error {
4747

4848
func templatesProvider() language.TemplateProvider {
4949
return func(name string) (string, error) {
50+
name = filepath.ToSlash(name)
5051
contents, err := dartTemplates.ReadFile(name)
5152
if err != nil {
5253
return "", err

internal/sidekick/internal/gcloud/command_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func TestCommandYAML(t *testing.T) {
4949
if err != nil {
5050
t.Fatalf("failed to read file: %v", err)
5151
}
52+
data = bytes.ReplaceAll(data, []byte("\r\n"), []byte("\n"))
53+
5254
if strings.Contains(string(data), "_PARTIALS_") {
5355
return
5456
}

internal/sidekick/internal/language/walk_templates_dir_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package language
1616

1717
import (
18+
"path/filepath"
1819
"testing"
1920

2021
"github.com/google/go-cmp/cmp"
@@ -26,11 +27,11 @@ func TestWalkDir(t *testing.T) {
2627
want := []GeneratedFile{
2728
{
2829
TemplatePath: "testTemplates/README.md.mustache",
29-
OutputPath: "/README.md",
30+
OutputPath: filepath.FromSlash("/README.md"),
3031
},
3132
{
3233
TemplatePath: "testTemplates/test001.txt.mustache",
33-
OutputPath: "/test001.txt",
34+
OutputPath: filepath.FromSlash("/test001.txt"),
3435
},
3536
}
3637
if diff := cmp.Diff(want, got); len(diff) != 0 {

internal/sidekick/internal/protobuf/input_files.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func applyIncludeList(files map[string]bool, sourceDirectory string, options map
9595
// Ignore any discovered paths, only the paths from the include list apply.
9696
clear(files)
9797
for _, p := range strings.Split(list, ",") {
98-
files[path.Join(sourceDirectory, p)] = true
98+
files[filepath.ToSlash(path.Join(sourceDirectory, p))] = true
9999
}
100100
}
101101

@@ -105,6 +105,6 @@ func applyExcludeList(files map[string]bool, sourceDirectory string, options map
105105
return
106106
}
107107
for _, p := range strings.Split(list, ",") {
108-
delete(files, path.Join(sourceDirectory, p))
108+
delete(files, filepath.ToSlash(path.Join(sourceDirectory, p)))
109109
}
110110
}

internal/sidekick/internal/protobuf/input_files_test.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ func TestBasic(t *testing.T) {
3939
if err != nil {
4040
t.Fatal(err)
4141
}
42+
for i := range got {
43+
got[i] = filepath.ToSlash(got[i])
44+
}
4245
want := []string{
43-
path.Join(testdataDir, source, "resources.proto"),
44-
path.Join(testdataDir, source, "service.proto"),
46+
filepath.ToSlash(path.Join(testdataDir, source, "resources.proto")),
47+
filepath.ToSlash(path.Join(testdataDir, source, "service.proto")),
4548
}
4649
if diff := cmp.Diff(want, got); len(diff) != 0 {
4750
t.Errorf("mismatched merged config (-want, +got):\n%s", diff)
@@ -71,8 +74,11 @@ func TestIncludeList(t *testing.T) {
7174
if err != nil {
7275
t.Fatal(err)
7376
}
77+
for i := range got {
78+
got[i] = filepath.ToSlash(got[i])
79+
}
7480
want := []string{
75-
path.Join(testdataDir, source, "resources.proto"),
81+
filepath.ToSlash(path.Join(testdataDir, source, "resources.proto")),
7682
}
7783
if diff := cmp.Diff(want, got); len(diff) != 0 {
7884
t.Errorf("mismatched merged config (-want, +got):\n%s", diff)
@@ -89,8 +95,11 @@ func TestExcludeList(t *testing.T) {
8995
if err != nil {
9096
t.Fatal(err)
9197
}
98+
for i := range got {
99+
got[i] = filepath.ToSlash(got[i])
100+
}
92101
want := []string{
93-
path.Join(testdataDir, source, "service.proto"),
102+
filepath.ToSlash(path.Join(testdataDir, source, "service.proto")),
94103
}
95104
if diff := cmp.Diff(want, got); len(diff) != 0 {
96105
t.Errorf("mismatched merged config (-want, +got):\n%s", diff)

internal/sidekick/refreshall.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func findAllDirectories(config *config.Config) ([]string, error) {
129129
}
130130

131131
func isInPath(dir, path string) bool {
132+
path = filepath.FromSlash(path)
132133
components := strings.Split(path, string(filepath.Separator))
133134
return slices.Contains(components, dir)
134135
}

0 commit comments

Comments
 (0)