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" |
Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 19 | "strconv" |
Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | var testCases = []struct { |
| 24 | in, out string |
| 25 | }{ |
| 26 | { |
| 27 | in: "File.java:40: error: cannot find symbol\n", |
| 28 | out: "\x1b[1mFile.java:40: \x1b[31merror:\x1b[0m\x1b[1m cannot find symbol\x1b[0m\n", |
| 29 | }, |
| 30 | { |
| 31 | in: "import static com.blah.SYMBOL;\n", |
| 32 | out: "import static com.blah.SYMBOL;\n", |
| 33 | }, |
| 34 | { |
| 35 | in: " ^ \n", |
| 36 | out: "\x1b[1m \x1b[32m^\x1b[0m\x1b[1m \x1b[0m\n", |
| 37 | }, |
| 38 | { |
| 39 | in: "File.java:398: warning: [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\n", |
| 40 | out: "\x1b[1mFile.java:398: \x1b[35mwarning:\x1b[0m\x1b[1m [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\x1b[0m\n", |
| 41 | }, |
| 42 | { |
Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 43 | in: "warning: [options] bootstrap class path not set in conjunction with -source 1.7\n", |
| 44 | out: "\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m [options] bootstrap class path not set in conjunction with -source 1.7\x1b[0m\n", |
| 45 | }, |
| 46 | { |
Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 47 | in: " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n", |
| 48 | out: " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n", |
| 49 | }, |
| 50 | { |
| 51 | in: ` |
| 52 | Note: Some input files use or override a deprecated API. |
| 53 | Note: Recompile with -Xlint:deprecation for details. |
| 54 | Note: Some input files use unchecked or unsafe operations. |
| 55 | Note: Recompile with -Xlint:unchecked for details. |
| 56 | Note: dir/file.java uses or overrides a deprecated API. |
| 57 | Note: dir/file.java uses unchecked or unsafe operations. |
| 58 | `, |
| 59 | out: "\n", |
| 60 | }, |
| 61 | { |
| 62 | in: "\n", |
| 63 | out: "\n", |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | func TestJavacColorize(t *testing.T) { |
Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 68 | for i, test := range testCases { |
| 69 | t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 70 | buf := new(bytes.Buffer) |
| 71 | err := process(bytes.NewReader([]byte(test.in)), buf) |
| 72 | if err != nil { |
| 73 | t.Errorf("error: %q", err) |
| 74 | } |
| 75 | got := string(buf.Bytes()) |
| 76 | if got != test.out { |
| 77 | t.Errorf("expected %q got %q", test.out, got) |
| 78 | } |
| 79 | }) |
Colin Cross | 521534f | 2017-02-07 23:25:30 -0800 | [diff] [blame] | 80 | } |
| 81 | } |
Colin Cross | ca3e287 | 2017-04-17 15:11:05 -0700 | [diff] [blame] | 82 | |
| 83 | func TestSubprocess(t *testing.T) { |
| 84 | t.Run("failure", func(t *testing.T) { |
| 85 | exitCode, err := Main("test", []string{"sh", "-c", "exit 9"}) |
| 86 | if err != nil { |
| 87 | t.Fatal("unexpected error", err) |
| 88 | } |
| 89 | if exitCode != 9 { |
| 90 | t.Fatal("expected exit code 9, got", exitCode) |
| 91 | } |
| 92 | }) |
| 93 | |
| 94 | t.Run("signal", func(t *testing.T) { |
| 95 | exitCode, err := Main("test", []string{"sh", "-c", "kill -9 $$"}) |
| 96 | if err != nil { |
| 97 | t.Fatal("unexpected error", err) |
| 98 | } |
| 99 | if exitCode != 137 { |
| 100 | t.Fatal("expected exit code 137, got", exitCode) |
| 101 | } |
| 102 | }) |
| 103 | |
| 104 | t.Run("success", func(t *testing.T) { |
| 105 | exitCode, err := Main("test", []string{"echo"}) |
| 106 | if err != nil { |
| 107 | t.Fatal("unexpected error", err) |
| 108 | } |
| 109 | if exitCode != 0 { |
| 110 | t.Fatal("expected exit code 0, got", exitCode) |
| 111 | } |
| 112 | }) |
| 113 | |
| 114 | } |