blob: c345479395d968084c1bb0a038a34e15e4cfa807 [file] [log] [blame]
Colin Cross521534f2017-02-07 23:25:30 -08001// 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
15package main
16
17import (
18 "bytes"
Dan Willemsen57a52382017-04-20 10:39:38 -070019 "io/ioutil"
Colin Crossca3e2872017-04-17 15:11:05 -070020 "strconv"
Colin Cross521534f2017-02-07 23:25:30 -080021 "testing"
22)
23
24var 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 Crossca3e2872017-04-17 15:11:05 -070044 in: "warning: [options] bootstrap class path not set in conjunction with -source 1.7\n",
45 out: "\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m [options] bootstrap class path not set in conjunction with -source 1.7\x1b[0m\n",
46 },
47 {
Colin Cross521534f2017-02-07 23:25:30 -080048 in: " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n",
49 out: " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n",
50 },
51 {
52 in: `
53Note: Some input files use or override a deprecated API.
54Note: Recompile with -Xlint:deprecation for details.
55Note: Some input files use unchecked or unsafe operations.
56Note: Recompile with -Xlint:unchecked for details.
57Note: dir/file.java uses or overrides a deprecated API.
58Note: dir/file.java uses unchecked or unsafe operations.
59`,
60 out: "\n",
61 },
62 {
63 in: "\n",
64 out: "\n",
65 },
66}
67
68func TestJavacColorize(t *testing.T) {
Colin Crossca3e2872017-04-17 15:11:05 -070069 for i, test := range testCases {
70 t.Run(strconv.Itoa(i), func(t *testing.T) {
71 buf := new(bytes.Buffer)
72 err := process(bytes.NewReader([]byte(test.in)), buf)
73 if err != nil {
74 t.Errorf("error: %q", err)
75 }
76 got := string(buf.Bytes())
77 if got != test.out {
78 t.Errorf("expected %q got %q", test.out, got)
79 }
80 })
Colin Cross521534f2017-02-07 23:25:30 -080081 }
82}
Colin Crossca3e2872017-04-17 15:11:05 -070083
84func TestSubprocess(t *testing.T) {
85 t.Run("failure", func(t *testing.T) {
Dan Willemsen57a52382017-04-20 10:39:38 -070086 exitCode, err := Main(ioutil.Discard, "test", []string{"sh", "-c", "exit 9"})
Colin Crossca3e2872017-04-17 15:11:05 -070087 if err != nil {
88 t.Fatal("unexpected error", err)
89 }
90 if exitCode != 9 {
91 t.Fatal("expected exit code 9, got", exitCode)
92 }
93 })
94
95 t.Run("signal", func(t *testing.T) {
Dan Willemsen57a52382017-04-20 10:39:38 -070096 exitCode, err := Main(ioutil.Discard, "test", []string{"sh", "-c", "kill -9 $$"})
Colin Crossca3e2872017-04-17 15:11:05 -070097 if err != nil {
98 t.Fatal("unexpected error", err)
99 }
100 if exitCode != 137 {
101 t.Fatal("expected exit code 137, got", exitCode)
102 }
103 })
104
105 t.Run("success", func(t *testing.T) {
Dan Willemsen57a52382017-04-20 10:39:38 -0700106 exitCode, err := Main(ioutil.Discard, "test", []string{"echo"})
Colin Crossca3e2872017-04-17 15:11:05 -0700107 if err != nil {
108 t.Fatal("unexpected error", err)
109 }
110 if exitCode != 0 {
111 t.Fatal("expected exit code 0, got", exitCode)
112 }
113 })
114
115}