| Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | package main | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "bytes" | 
| Dan Willemsen | 57a5238 | 2017-04-20 10:39:38 -0700 | [diff] [blame] | 19 | "io/ioutil" | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 20 | "strconv" | 
| Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 21 | "testing" | 
|  | 22 | ) | 
|  | 23 |  | 
|  | 24 | var testCases = []struct { | 
|  | 25 | in, out string | 
|  | 26 | }{ | 
|  | 27 | { | 
|  | 28 | in:  "File.java:40: error: cannot find symbol\n", | 
|  | 29 | out: "\x1b[1mFile.java:40: \x1b[31merror:\x1b[0m\x1b[1m cannot find symbol\x1b[0m\n", | 
|  | 30 | }, | 
|  | 31 | { | 
|  | 32 | in:  "import static com.blah.SYMBOL;\n", | 
|  | 33 | out: "import static com.blah.SYMBOL;\n", | 
|  | 34 | }, | 
|  | 35 | { | 
|  | 36 | in:  "          ^           \n", | 
|  | 37 | out: "\x1b[1m          \x1b[32m^\x1b[0m\x1b[1m           \x1b[0m\n", | 
|  | 38 | }, | 
|  | 39 | { | 
|  | 40 | in:  "File.java:398: warning: [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\n", | 
|  | 41 | out: "\x1b[1mFile.java:398: \x1b[35mwarning:\x1b[0m\x1b[1m [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\x1b[0m\n", | 
|  | 42 | }, | 
|  | 43 | { | 
| Colin Cross | ae96e53 | 2017-10-17 13:57:11 -0700 | [diff] [blame] | 44 | in:  "warning: [options] blah\n", | 
|  | 45 | out: "\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m [options] blah\x1b[0m\n", | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 46 | }, | 
|  | 47 | { | 
| Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 48 | in:  "    (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n", | 
|  | 49 | out: "    (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n", | 
|  | 50 | }, | 
|  | 51 | { | 
|  | 52 | in: ` | 
|  | 53 | Note: Some input files use or override a deprecated API. | 
|  | 54 | Note: Recompile with -Xlint:deprecation for details. | 
|  | 55 | Note: Some input files use unchecked or unsafe operations. | 
|  | 56 | Note: Recompile with -Xlint:unchecked for details. | 
|  | 57 | Note: dir/file.java uses or overrides a deprecated API. | 
|  | 58 | Note: dir/file.java uses unchecked or unsafe operations. | 
| Colin Cross | ae96e53 | 2017-10-17 13:57:11 -0700 | [diff] [blame] | 59 | warning: [options] bootstrap class path not set in conjunction with -source 1.7 | 
| Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 60 | `, | 
|  | 61 | out: "\n", | 
|  | 62 | }, | 
|  | 63 | { | 
|  | 64 | in:  "\n", | 
|  | 65 | out: "\n", | 
|  | 66 | }, | 
| Nan Zhang | 40b41b4 | 2018-10-02 16:11:17 -0700 | [diff] [blame] | 67 | { | 
|  | 68 | in: ` | 
|  | 69 | javadoc: warning - The old Doclet and Taglet APIs in the packages | 
|  | 70 | com.sun.javadoc, com.sun.tools.doclets and their implementations | 
|  | 71 | are planned to be removed in a future JDK release. These | 
|  | 72 | components have been superseded by the new APIs in jdk.javadoc.doclet. | 
|  | 73 | Users are strongly recommended to migrate to the new APIs. | 
|  | 74 | javadoc: option --boot-class-path not allowed with target 1.9 | 
|  | 75 | `, | 
|  | 76 | out: "\n", | 
|  | 77 | }, | 
| Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 78 | } | 
|  | 79 |  | 
|  | 80 | func TestJavacColorize(t *testing.T) { | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 81 | for i, test := range testCases { | 
|  | 82 | t.Run(strconv.Itoa(i), func(t *testing.T) { | 
|  | 83 | buf := new(bytes.Buffer) | 
|  | 84 | err := process(bytes.NewReader([]byte(test.in)), buf) | 
|  | 85 | if err != nil { | 
|  | 86 | t.Errorf("error: %q", err) | 
|  | 87 | } | 
|  | 88 | got := string(buf.Bytes()) | 
|  | 89 | if got != test.out { | 
|  | 90 | t.Errorf("expected %q got %q", test.out, got) | 
|  | 91 | } | 
|  | 92 | }) | 
| Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 93 | } | 
|  | 94 | } | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | func TestSubprocess(t *testing.T) { | 
|  | 97 | t.Run("failure", func(t *testing.T) { | 
| Dan Willemsen | 57a5238 | 2017-04-20 10:39:38 -0700 | [diff] [blame] | 98 | exitCode, err := Main(ioutil.Discard, "test", []string{"sh", "-c", "exit 9"}) | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 99 | if err != nil { | 
|  | 100 | t.Fatal("unexpected error", err) | 
|  | 101 | } | 
|  | 102 | if exitCode != 9 { | 
|  | 103 | t.Fatal("expected exit code 9, got", exitCode) | 
|  | 104 | } | 
|  | 105 | }) | 
|  | 106 |  | 
|  | 107 | t.Run("signal", func(t *testing.T) { | 
| Dan Willemsen | 57a5238 | 2017-04-20 10:39:38 -0700 | [diff] [blame] | 108 | exitCode, err := Main(ioutil.Discard, "test", []string{"sh", "-c", "kill -9 $$"}) | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 109 | if err != nil { | 
|  | 110 | t.Fatal("unexpected error", err) | 
|  | 111 | } | 
|  | 112 | if exitCode != 137 { | 
|  | 113 | t.Fatal("expected exit code 137, got", exitCode) | 
|  | 114 | } | 
|  | 115 | }) | 
|  | 116 |  | 
|  | 117 | t.Run("success", func(t *testing.T) { | 
| Dan Willemsen | 57a5238 | 2017-04-20 10:39:38 -0700 | [diff] [blame] | 118 | exitCode, err := Main(ioutil.Discard, "test", []string{"echo"}) | 
| Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 119 | if err != nil { | 
|  | 120 | t.Fatal("unexpected error", err) | 
|  | 121 | } | 
|  | 122 | if exitCode != 0 { | 
|  | 123 | t.Fatal("expected exit code 0, got", exitCode) | 
|  | 124 | } | 
|  | 125 | }) | 
|  | 126 |  | 
|  | 127 | } |