2017-02-08 15:25:30 +08:00
// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
2017-04-21 01:39:38 +08:00
"io/ioutil"
2017-04-18 06:11:05 +08:00
"strconv"
2017-02-08 15:25:30 +08:00
"testing"
)
var testCases = [ ] struct {
in , out string
} {
{
in : "File.java:40: error: cannot find symbol\n" ,
out : "\x1b[1mFile.java:40: \x1b[31merror:\x1b[0m\x1b[1m cannot find symbol\x1b[0m\n" ,
} ,
{
in : "import static com.blah.SYMBOL;\n" ,
out : "import static com.blah.SYMBOL;\n" ,
} ,
{
in : " ^ \n" ,
out : "\x1b[1m \x1b[32m^\x1b[0m\x1b[1m \x1b[0m\n" ,
} ,
{
in : "File.java:398: warning: [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\n" ,
out : "\x1b[1mFile.java:398: \x1b[35mwarning:\x1b[0m\x1b[1m [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\x1b[0m\n" ,
} ,
2017-04-18 06:11:05 +08:00
{
2017-10-18 04:57:11 +08:00
in : "warning: [options] blah\n" ,
out : "\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m [options] blah\x1b[0m\n" ,
2017-04-18 06:11:05 +08:00
} ,
2017-02-08 15:25:30 +08:00
{
in : " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n" ,
out : " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n" ,
} ,
{
in : `
Note : Some input files use or override a deprecated API .
Note : Recompile with - Xlint : deprecation for details .
Note : Some input files use unchecked or unsafe operations .
Note : Recompile with - Xlint : unchecked for details .
Note : dir / file . java uses or overrides a deprecated API .
Note : dir / file . java uses unchecked or unsafe operations .
2017-10-18 04:57:11 +08:00
warning : [ options ] bootstrap class path not set in conjunction with - source 1.7
2017-02-08 15:25:30 +08:00
` ,
out : "\n" ,
} ,
{
in : "\n" ,
out : "\n" ,
} ,
2018-10-03 07:11:17 +08:00
{
in : `
javadoc : warning - The old Doclet and Taglet APIs in the packages
com . sun . javadoc , com . sun . tools . doclets and their implementations
are planned to be removed in a future JDK release . These
components have been superseded by the new APIs in jdk . javadoc . doclet .
Users are strongly recommended to migrate to the new APIs .
javadoc : option -- boot - class - path not allowed with target 1.9
` ,
out : "\n" ,
} ,
2019-11-12 05:07:38 +08:00
{
in : `
warning : [ options ] bootstrap class path not set in conjunction with - source 1.9 \ n
1 warning
` ,
out : "\n" ,
} ,
{
in : `
warning : foo
warning : [ options ] bootstrap class path not set in conjunction with - source 1.9 \ n
2 warnings
` ,
out : "\n\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m foo\x1b[0m\n1 warning\n" ,
} ,
2017-02-08 15:25:30 +08:00
}
func TestJavacColorize ( t * testing . T ) {
2017-04-18 06:11:05 +08:00
for i , test := range testCases {
t . Run ( strconv . Itoa ( i ) , func ( t * testing . T ) {
buf := new ( bytes . Buffer )
2019-11-12 05:07:38 +08:00
proc := processor { }
err := proc . process ( bytes . NewReader ( [ ] byte ( test . in ) ) , buf )
2017-04-18 06:11:05 +08:00
if err != nil {
t . Errorf ( "error: %q" , err )
}
got := string ( buf . Bytes ( ) )
if got != test . out {
t . Errorf ( "expected %q got %q" , test . out , got )
}
} )
}
}
func TestSubprocess ( t * testing . T ) {
t . Run ( "failure" , func ( t * testing . T ) {
2017-04-21 01:39:38 +08:00
exitCode , err := Main ( ioutil . Discard , "test" , [ ] string { "sh" , "-c" , "exit 9" } )
2017-02-08 15:25:30 +08:00
if err != nil {
2017-04-18 06:11:05 +08:00
t . Fatal ( "unexpected error" , err )
2017-02-08 15:25:30 +08:00
}
2017-04-18 06:11:05 +08:00
if exitCode != 9 {
t . Fatal ( "expected exit code 9, got" , exitCode )
2017-02-08 15:25:30 +08:00
}
2017-04-18 06:11:05 +08:00
} )
t . Run ( "signal" , func ( t * testing . T ) {
2017-04-21 01:39:38 +08:00
exitCode , err := Main ( ioutil . Discard , "test" , [ ] string { "sh" , "-c" , "kill -9 $$" } )
2017-04-18 06:11:05 +08:00
if err != nil {
t . Fatal ( "unexpected error" , err )
}
if exitCode != 137 {
t . Fatal ( "expected exit code 137, got" , exitCode )
}
} )
t . Run ( "success" , func ( t * testing . T ) {
2017-04-21 01:39:38 +08:00
exitCode , err := Main ( ioutil . Discard , "test" , [ ] string { "echo" } )
2017-04-18 06:11:05 +08:00
if err != nil {
t . Fatal ( "unexpected error" , err )
}
if exitCode != 0 {
t . Fatal ( "expected exit code 0, got" , exitCode )
}
} )
2017-02-08 15:25:30 +08:00
}