1// Copyright 2019 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 status 16 17import ( 18 "io/ioutil" 19 "os" 20 "path/filepath" 21 "testing" 22 "time" 23 24 "android/soong/ui/logger" 25) 26 27// Tests that closing the ninja reader when nothing has opened the other end of the fifo is fast. 28func TestNinjaReader_Close(t *testing.T) { 29 tempDir, err := ioutil.TempDir("", "ninja_test") 30 if err != nil { 31 t.Fatal(err) 32 } 33 defer os.RemoveAll(tempDir) 34 35 stat := &Status{} 36 nr := NewNinjaReader(logger.New(ioutil.Discard), stat.StartTool(), filepath.Join(tempDir, "fifo")) 37 38 start := time.Now() 39 40 nr.Close() 41 42 if g, w := time.Since(start), NINJA_READER_CLOSE_TIMEOUT; g >= w { 43 t.Errorf("nr.Close timed out, %s > %s", g, w) 44 } 45} 46 47// Test that error hint is added to output if available 48func TestNinjaReader_CorrectErrorHint(t *testing.T) { 49 errorPattern1 := "pattern-1 in input" 50 errorHint1 := "\n Fix by doing task 1" 51 errorPattern2 := "pattern-2 in input" 52 errorHint2 := "\n Fix by doing task 2" 53 mockErrorHints := make(map[string]string) 54 mockErrorHints[errorPattern1] = errorHint1 55 mockErrorHints[errorPattern2] = errorHint2 56 57 errorHintGenerator := *newErrorHintGenerator(mockErrorHints) 58 testCases := []struct { 59 rawOutput string 60 buildExitCode int 61 expectedFinalOutput string 62 testCaseErrorMessage string 63 }{ 64 { 65 rawOutput: "ninja build was successful", 66 buildExitCode: 0, 67 expectedFinalOutput: "ninja build was successful", 68 testCaseErrorMessage: "raw output changed when build was successful", 69 }, 70 { 71 rawOutput: "ninja build failed", 72 buildExitCode: 1, 73 expectedFinalOutput: "ninja build failed", 74 testCaseErrorMessage: "raw output changed even when no error hint pattern was found", 75 }, 76 { 77 rawOutput: "ninja build failed: " + errorPattern1 + "some footnotes", 78 buildExitCode: 1, 79 expectedFinalOutput: "ninja build failed: " + errorPattern1 + "some footnotes" + errorHint1, 80 testCaseErrorMessage: "error hint not added despite pattern match", 81 }, 82 { 83 rawOutput: "ninja build failed: " + errorPattern2 + errorPattern1, 84 buildExitCode: 1, 85 expectedFinalOutput: "ninja build failed: " + errorPattern2 + errorPattern1 + errorHint2, 86 testCaseErrorMessage: "error hint should be added for first pattern match in raw output", 87 }, 88 } 89 for _, testCase := range testCases { 90 actualFinalOutput := errorHintGenerator.GetOutputWithErrorHint(testCase.rawOutput, testCase.buildExitCode) 91 if actualFinalOutput != testCase.expectedFinalOutput { 92 t.Errorf(testCase.testCaseErrorMessage+"\nexpected: %s\ngot: %s", testCase.expectedFinalOutput, actualFinalOutput) 93 } 94 } 95} 96