Skip to content

Commit d595fbd

Browse files
bsheedy-workDawn LUCI CQ
authored andcommitted
Remove os usage from get-test-plan
Removes usage of the os module for filesystem-related functions from //tools/src/cmd/get-test-plan/main.go in favor of dependency injection. Test coverage will be added in a follow-up CL. Bug: 344014313 Change-Id: I8bcc739dd445acbe5fbaa5e3f4ad049476e184ea Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/282855 Commit-Queue: dan sinclair <dsinclair@chromium.org> Auto-Submit: Brian Sheedy <bsheedy@google.com> Reviewed-by: dan sinclair <dsinclair@chromium.org>
1 parent 06c9e44 commit d595fbd

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

tools/src/cmd/get-test-plan/main.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import (
5454
"strconv"
5555
"strings"
5656

57+
"dawn.googlesource.com/dawn/tools/src/oswrapper"
5758
"golang.org/x/net/html"
5859
)
5960

@@ -108,7 +109,8 @@ func main() {
108109
flag.PrintDefaults()
109110
}
110111

111-
err := run()
112+
osWrapper := oswrapper.GetRealOSWrapper()
113+
err := run(osWrapper)
112114
if err != nil {
113115
if errors.Is(err, errInvalidArg) {
114116
fmt.Fprintf(os.Stderr, "Error: %v\n\n", err)
@@ -120,7 +122,9 @@ func main() {
120122
}
121123
}
122124

123-
func run() error {
125+
// TODO(crbug.com/473064729): Add unittests once HTTP requests are handled via
126+
// dependency injection.
127+
func run(osWrapper oswrapper.OSWrapper) error {
124128
// Parse flags
125129
keyword := flag.String("keyword", "",
126130
`if provided, it will be used as the keyword to search WGSL spec for rules
@@ -147,7 +151,7 @@ contains the provided string`)
147151
args := flag.Args()
148152

149153
// Parse spec
150-
spec, err := parseSpec(args)
154+
spec, err := parseSpec(args, osWrapper)
151155
if err != nil {
152156
return err
153157
}
@@ -164,7 +168,7 @@ contains the provided string`)
164168
rules := parser.rules
165169

166170
if *ctsDir != "" {
167-
err := getUnimplementedTestPlan(*parser, *ctsDir)
171+
err := getUnimplementedTestPlan(*parser, *ctsDir, osWrapper)
168172
if err != nil {
169173
return err
170174
}
@@ -180,11 +184,11 @@ contains the provided string`)
180184
if err != nil {
181185
return err
182186
}
183-
return writeFile(*output, string(j))
187+
return writeFile(*output, string(j), osWrapper)
184188
} else if strings.HasSuffix(*output, ".txt") {
185-
return writeFile(*output, txt)
189+
return writeFile(*output, txt, osWrapper)
186190
} else if strings.HasSuffix(*output, ".tsv") {
187-
return writeFile(*output, tsv)
191+
return writeFile(*output, tsv, osWrapper)
188192
} else {
189193
return fmt.Errorf("unsupported output file extension: %v", *output)
190194
}
@@ -298,18 +302,20 @@ func concatRules(rules []rule, testNameFilter string) (string, string) {
298302

299303
// writeFile writes content to path
300304
// the existing content will be overwritten
301-
func writeFile(path, content string) error {
302-
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
305+
func writeFile(path, content string, fsWriter oswrapper.FilesystemWriter) error {
306+
if err := fsWriter.MkdirAll(filepath.Dir(path), 0777); err != nil {
303307
return fmt.Errorf("failed to create directory for '%v': %w", path, err)
304308
}
305-
if err := os.WriteFile(path, []byte(content), 0666); err != nil {
309+
if err := fsWriter.WriteFile(path, []byte(content), 0666); err != nil {
306310
return fmt.Errorf("failed to write file '%v': %w", path, err)
307311
}
308312
return nil
309313
}
310314

315+
// TODO(crbug.com/473064729): Add unittest coverage once HTTP requests are
316+
// handled via dependency injection.
311317
// parseSpec reads the spec from a local file, or the URL to WGSL spec
312-
func parseSpec(args []string) (*html.Node, error) {
318+
func parseSpec(args []string, fsReader oswrapper.FilesystemReader) (*html.Node, error) {
313319
// Check for explicit WGSL spec path
314320
specURL, _ := url.Parse(specPath)
315321
switch len(args) {
@@ -347,7 +353,7 @@ func parseSpec(args []string) (*html.Node, error) {
347353
return nil, fmt.Errorf("failed to load the WGSL spec from '%v': %w", specURL, err)
348354
}
349355

350-
file, err := os.Open(path)
356+
file, err := fsReader.Open(path)
351357
if err != nil {
352358
return nil, fmt.Errorf("failed to load the WGSL spec from '%v': %w", specURL, err)
353359
}
@@ -371,7 +377,7 @@ func parseSpec(args []string) (*html.Node, error) {
371377
if err != nil {
372378
return nil, fmt.Errorf("failed to load the WGSL spec from '%v': %w", specURL, err)
373379
}
374-
file, err := os.Open(path)
380+
file, err := fsReader.Open(path)
375381
if err != nil {
376382
return nil, fmt.Errorf("failed to load the WGSL spec from '%v': %w", specURL, err)
377383
}
@@ -838,17 +844,17 @@ func getSha1(a string, b string) (string, error) {
838844

839845
// getUnimplementedPlan generate the typescript code of a test plan for rules in sections[start, end]
840846
// then it writes the generated test plans in the given 'path'
841-
func getUnimplementedTestPlan(p Parser, path string) error {
847+
func getUnimplementedTestPlan(p Parser, path string, fsWriter oswrapper.FilesystemWriter) error {
842848
rules := p.rules
843849
start := p.firstSectionContainingRule
844850
end := p.lastSectionContainingRule
845851
validationPath := filepath.Join(path, "validation")
846-
if err := validationTestPlan(rules, validationPath, start, end); err != nil {
852+
if err := validationTestPlan(rules, validationPath, start, end, fsWriter); err != nil {
847853
return err
848854
}
849855

850856
executionPath := filepath.Join(path, "execution", "builtin")
851-
if err := executionTestPlan(rules, executionPath); err != nil {
857+
if err := executionTestPlan(rules, executionPath, fsWriter); err != nil {
852858
return err
853859
}
854860
return nil
@@ -876,7 +882,7 @@ func getTestPlanFilePath(path string, x, y, digits int) (string, error) {
876882
}
877883

878884
// validationTestPlan generates the typescript code of a test plan for rules in sections[start, end]
879-
func validationTestPlan(rules []rule, path string, start int, end int) error {
885+
func validationTestPlan(rules []rule, path string, start int, end int, fsWriter oswrapper.FilesystemWriter) error {
880886
content := [][]string{}
881887
filePath := []string{}
882888
for section := 0; section <= end; section++ {
@@ -906,7 +912,7 @@ func validationTestPlan(rules []rule, path string, start int, end int) error {
906912

907913
for i := start; i <= end; i++ {
908914
if len(content[i]) > 1 {
909-
if err := writeFile(filePath[i], strings.Join(content[i], "\n")); err != nil {
915+
if err := writeFile(filePath[i], strings.Join(content[i], "\n"), fsWriter); err != nil {
910916
return err
911917
}
912918
}
@@ -917,7 +923,7 @@ func validationTestPlan(rules []rule, path string, start int, end int) error {
917923

918924
// executionTestPlan generates the typescript code of a test plan for rules in the given section
919925
// the rules in section X.Y.* will be written to path/sectionX_Y.spec.ts
920-
func executionTestPlan(rules []rule, path string) error {
926+
func executionTestPlan(rules []rule, path string, fsWriter oswrapper.FilesystemWriter) error {
921927
// TODO(SarahM) This generates execution tests for builtin function tests. Add other executions tests.
922928
section, err := getBuiltinSectionNum(rules)
923929
if err != nil {
@@ -974,7 +980,7 @@ func executionTestPlan(rules []rule, path string) error {
974980
// Write the file if there is a test in there
975981
// compared with >1 because content has at least the test description
976982
if len(content[i]) > 1 {
977-
if err := writeFile(filePath[i], strings.Join(content[i], "\n")); err != nil {
983+
if err := writeFile(filePath[i], strings.Join(content[i], "\n"), fsWriter); err != nil {
978984
return err
979985
}
980986
}

0 commit comments

Comments
 (0)