Home | History | Annotate | Download | only in build
      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 build
     16 
     17 import "testing"
     18 
     19 func TestStripAnsiEscapes(t *testing.T) {
     20 	testcases := []struct {
     21 		input  string
     22 		output string
     23 	}{
     24 		{
     25 			"",
     26 			"",
     27 		},
     28 		{
     29 			"This is a test",
     30 			"This is a test",
     31 		},
     32 		{
     33 			"interrupted: \x1b[12",
     34 			"interrupted: ",
     35 		},
     36 		{
     37 			"other \x1bescape \x1b",
     38 			"other \x1bescape \x1b",
     39 		},
     40 		{ // from pretty-error macro
     41 			"\x1b[1mart/Android.mk: \x1b[31merror:\x1b[0m\x1b[1m art: test error \x1b[0m",
     42 			"art/Android.mk: error: art: test error ",
     43 		},
     44 		{ // from envsetup.sh make wrapper
     45 			"\x1b[0;31m#### make failed to build some targets (2 seconds) ####\x1b[00m",
     46 			"#### make failed to build some targets (2 seconds) ####",
     47 		},
     48 		{ // from clang (via ninja testcase)
     49 			"\x1b[1maffixmgr.cxx:286:15: \x1b[0m\x1b[0;1;35mwarning: \x1b[0m\x1b[1musing the result... [-Wparentheses]\x1b[0m",
     50 			"affixmgr.cxx:286:15: warning: using the result... [-Wparentheses]",
     51 		},
     52 	}
     53 	for _, tc := range testcases {
     54 		got := string(stripAnsiEscapes([]byte(tc.input)))
     55 		if got != tc.output {
     56 			t.Errorf("output strings didn't match\n"+
     57 				"input: %#v\n"+
     58 				" want: %#v\n"+
     59 				"  got: %#v", tc.input, tc.output, got)
     60 		}
     61 	}
     62 }
     63