ninja_test.go 3.2 KB

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