Skip to content

Commit 3136671

Browse files
bsheedy-workDawn LUCI CQ
authored andcommitted
Refactor idlgen for better testing
Slightly refactors //tools/src/cmd/idlgen/main.go to better accommodate future testing. Changes include: * Making showUsage return an error instead of calling os.Exit * Making run not rely on global flag state * Updating parsing code to read and parse a file instead of relying on ParseFiles, as that circumvents the filesystem dependency injection All of these changes should be functional no-ops outside of tests. Bug: 344014313 Change-Id: I7c22c1437b337df1714adab0f9e50fc2ec828527 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/282877 Reviewed-by: dan sinclair <dsinclair@chromium.org> Commit-Queue: dan sinclair <dsinclair@chromium.org> Auto-Submit: Brian Sheedy <bsheedy@google.com>
1 parent 181ff62 commit 3136671

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

tools/src/cmd/idlgen/main.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,37 @@ import (
4646
)
4747

4848
func main() {
49-
if err := run(oswrapper.GetRealOSWrapper()); err != nil {
49+
if err := run(os.Args[1:], oswrapper.GetRealOSWrapper()); err != nil {
5050
fmt.Println(err)
5151
os.Exit(1)
5252
}
5353
}
5454

55-
func showUsage() {
56-
fmt.Println(`
55+
func showUsage() error {
56+
return fmt.Errorf(`
5757
idlgen is a tool used to generate code from WebIDL files and a golang
5858
template file
5959
6060
Usage:
6161
idlgen --template=<template-path> --output=<output-path> <idl-file> [<idl-file>...]`)
62-
os.Exit(1)
6362
}
6463

65-
func run(osWrapper oswrapper.OSWrapper) error {
64+
func run(args []string, osWrapper oswrapper.OSWrapper) error {
6665
var templatePath string
6766
var outputPath string
68-
flag.StringVar(&templatePath, "template", "", "the template file run with the parsed WebIDL files")
69-
flag.StringVar(&outputPath, "output", "", "the output file")
70-
flag.Parse()
7167

72-
idlFiles := flag.Args()
68+
flagSet := flag.NewFlagSet("idlgen", flag.ContinueOnError)
69+
flagSet.StringVar(&templatePath, "template", "", "the template file run with the parsed WebIDL files")
70+
flagSet.StringVar(&outputPath, "output", "", "the output file")
71+
if err := flagSet.Parse(args); err != nil {
72+
return err
73+
}
74+
75+
idlFiles := flagSet.Args()
7376

7477
// Check all required arguments are provided
7578
if templatePath == "" || outputPath == "" || len(idlFiles) == 0 {
76-
showUsage()
79+
return showUsage()
7780
}
7881

7982
// Open up the output file
@@ -118,7 +121,10 @@ func run(osWrapper oswrapper.OSWrapper) error {
118121
}
119122

120123
// Initialize the generator
121-
g := generator{t: template.New(templatePath)}
124+
g := generator{
125+
t: template.New(templatePath),
126+
osWrapper: osWrapper,
127+
}
122128
g.workingDir = filepath.Dir(templatePath)
123129
g.funcs = map[string]interface{}{
124130
// Functions exposed to the template
@@ -446,6 +452,8 @@ type generator struct {
446452
funcs map[string]interface{}
447453
// dependency-sorted declarations
448454
declarations declarations
455+
// osWrapper for file operations
456+
osWrapper oswrapper.OSWrapper
449457
}
450458

451459
// eval executes the sub-template with the given name and arguments, returning
@@ -494,13 +502,25 @@ func (g *generator) lookup(name string) ast.Decl {
494502
// include loads the template with the given path, importing the declarations
495503
// into the scope of the current template.
496504
func (g *generator) include(path string) (string, error) {
497-
t, err := g.t.
505+
// The file is read and manually parsed instead of using Template.ParseFiles
506+
// since ParseFiles circumvents our filesystem dependency injection.
507+
fullPath := filepath.Join(g.workingDir, path)
508+
content, err := g.osWrapper.ReadFile(fullPath)
509+
if err != nil {
510+
return "", err
511+
}
512+
513+
// ParseFiles uses the basename of the file as the template name.
514+
basename := filepath.Base(path)
515+
t := g.t.New(basename).
498516
Option("missingkey=invalid").
499-
Funcs(g.funcs).
500-
ParseFiles(filepath.Join(g.workingDir, path))
517+
Funcs(g.funcs)
518+
519+
t, err = t.Parse(string(content))
501520
if err != nil {
502521
return "", err
503522
}
523+
504524
g.t.AddParseTree(path, t.Tree)
505525
return "", nil
506526
}

0 commit comments

Comments
 (0)