LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This test applies gofmt to all Go files under -root. | 5 // This test applies gofmt to all Go files under -root. |
6 // To test specific files provide a list of comma-separated | 6 // To test specific files provide a list of comma-separated |
7 // filenames via the -files flag: go test -files=gofmt.go . | 7 // filenames via the -files flag: go test -files=gofmt.go . |
8 | 8 |
9 package main | 9 package main |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 var ( | 26 var ( |
27 root = flag.String("root", runtime.GOROOT(), "test root directory") | 27 root = flag.String("root", runtime.GOROOT(), "test root directory") |
28 files = flag.String("files", "", "comma-separated list of files to tes
t") | 28 files = flag.String("files", "", "comma-separated list of files to tes
t") |
29 ngo = flag.Int("n", runtime.NumCPU(), "number of goroutines used") | 29 ngo = flag.Int("n", runtime.NumCPU(), "number of goroutines used") |
30 verbose = flag.Bool("verbose", false, "verbose mode") | 30 verbose = flag.Bool("verbose", false, "verbose mode") |
31 nfiles int // number of files processed | 31 nfiles int // number of files processed |
32 ) | 32 ) |
33 | 33 |
34 func gofmt(fset *token.FileSet, filename string, src *bytes.Buffer) error { | 34 func gofmt(fset *token.FileSet, filename string, src *bytes.Buffer) error { |
35 » f, _, err := parse(fset, filename, src.Bytes(), false) | 35 » f, _, _, err := parse(fset, filename, src.Bytes(), false) |
36 if err != nil { | 36 if err != nil { |
37 return err | 37 return err |
38 } | 38 } |
39 ast.SortImports(fset, f) | 39 ast.SortImports(fset, f) |
40 src.Reset() | 40 src.Reset() |
41 return (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(s
rc, fset, f) | 41 return (&printer.Config{Mode: printerMode, Tabwidth: tabWidth}).Fprint(s
rc, fset, f) |
42 } | 42 } |
43 | 43 |
44 func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) { | 44 func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) { |
45 // open file | 45 // open file |
46 f, err := os.Open(filename) | 46 f, err := os.Open(filename) |
47 if err != nil { | 47 if err != nil { |
48 t.Error(err) | 48 t.Error(err) |
49 return | 49 return |
50 } | 50 } |
51 | 51 |
52 // read file | 52 // read file |
53 b1.Reset() | 53 b1.Reset() |
54 _, err = io.Copy(b1, f) | 54 _, err = io.Copy(b1, f) |
55 f.Close() | 55 f.Close() |
56 if err != nil { | 56 if err != nil { |
57 t.Error(err) | 57 t.Error(err) |
58 return | 58 return |
59 } | 59 } |
60 | 60 |
61 // exclude files w/ syntax errors (typically test cases) | 61 // exclude files w/ syntax errors (typically test cases) |
62 fset := token.NewFileSet() | 62 fset := token.NewFileSet() |
63 » if _, _, err = parse(fset, filename, b1.Bytes(), false); err != nil { | 63 » if _, _, _, err = parse(fset, filename, b1.Bytes(), false); err != nil { |
64 if *verbose { | 64 if *verbose { |
65 fmt.Fprintf(os.Stderr, "ignoring %s\n", err) | 65 fmt.Fprintf(os.Stderr, "ignoring %s\n", err) |
66 } | 66 } |
67 return | 67 return |
68 } | 68 } |
69 | 69 |
70 // gofmt file | 70 // gofmt file |
71 if err = gofmt(fset, filename, b1); err != nil { | 71 if err = gofmt(fset, filename, b1); err != nil { |
72 t.Errorf("1st gofmt failed: %v", err) | 72 t.Errorf("1st gofmt failed: %v", err) |
73 return | 73 return |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 | 150 |
151 // wait for all test goroutines to complete | 151 // wait for all test goroutines to complete |
152 for i := 0; i < *ngo; i++ { | 152 for i := 0; i < *ngo; i++ { |
153 <-done | 153 <-done |
154 } | 154 } |
155 | 155 |
156 if *verbose { | 156 if *verbose { |
157 fmt.Printf("processed %d files\n", nfiles) | 157 fmt.Printf("processed %d files\n", nfiles) |
158 } | 158 } |
159 } | 159 } |
LEFT | RIGHT |