javac_wrapper_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package main
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "strconv"
  19. "testing"
  20. )
  21. var testCases = []struct {
  22. in, out string
  23. }{
  24. {
  25. in: "File.java:40: error: cannot find symbol\n",
  26. out: "\x1b[1mFile.java:40: \x1b[31merror:\x1b[0m\x1b[1m cannot find symbol\x1b[0m\n",
  27. },
  28. {
  29. in: "import static com.blah.SYMBOL;\n",
  30. out: "import static com.blah.SYMBOL;\n",
  31. },
  32. {
  33. in: " ^ \n",
  34. out: "\x1b[1m \x1b[32m^\x1b[0m\x1b[1m \x1b[0m\n",
  35. },
  36. {
  37. in: "File.java:398: warning: [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\n",
  38. out: "\x1b[1mFile.java:398: \x1b[35mwarning:\x1b[0m\x1b[1m [RectIntersectReturnValueIgnored] Return value of com.blah.function() must be checked\x1b[0m\n",
  39. },
  40. {
  41. in: "warning: [options] blah\n",
  42. out: "\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m [options] blah\x1b[0m\n",
  43. },
  44. {
  45. in: " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n",
  46. out: " (see http://go/errorprone/bugpattern/RectIntersectReturnValueIgnored.md)\n",
  47. },
  48. {
  49. in: `
  50. Note: Some input files use or override a deprecated API.
  51. Note: Recompile with -Xlint:deprecation for details.
  52. Note: Some input files use unchecked or unsafe operations.
  53. Note: Recompile with -Xlint:unchecked for details.
  54. Note: dir/file.java uses or overrides a deprecated API.
  55. Note: dir/file.java uses unchecked or unsafe operations.
  56. warning: [options] bootstrap class path not set in conjunction with -source 1.7
  57. `,
  58. out: "\n",
  59. },
  60. {
  61. in: "\n",
  62. out: "\n",
  63. },
  64. {
  65. in: `
  66. javadoc: warning - The old Doclet and Taglet APIs in the packages
  67. com.sun.javadoc, com.sun.tools.doclets and their implementations
  68. are planned to be removed in a future JDK release. These
  69. components have been superseded by the new APIs in jdk.javadoc.doclet.
  70. Users are strongly recommended to migrate to the new APIs.
  71. javadoc: option --boot-class-path not allowed with target 1.9
  72. `,
  73. out: "\n",
  74. },
  75. {
  76. in: `
  77. warning: [options] bootstrap class path not set in conjunction with -source 1.9\n
  78. 1 warning
  79. `,
  80. out: "\n",
  81. },
  82. {
  83. in: `
  84. warning: foo
  85. warning: [options] bootstrap class path not set in conjunction with -source 1.9\n
  86. 2 warnings
  87. `,
  88. out: "\n\x1b[1m\x1b[35mwarning:\x1b[0m\x1b[1m foo\x1b[0m\n1 warning\n",
  89. },
  90. }
  91. func TestJavacColorize(t *testing.T) {
  92. for i, test := range testCases {
  93. t.Run(strconv.Itoa(i), func(t *testing.T) {
  94. buf := new(bytes.Buffer)
  95. proc := processor{}
  96. err := proc.process(bytes.NewReader([]byte(test.in)), buf)
  97. if err != nil {
  98. t.Errorf("error: %q", err)
  99. }
  100. got := string(buf.Bytes())
  101. if got != test.out {
  102. t.Errorf("expected %q got %q", test.out, got)
  103. }
  104. })
  105. }
  106. }
  107. func TestSubprocess(t *testing.T) {
  108. t.Run("failure", func(t *testing.T) {
  109. exitCode, err := Main(ioutil.Discard, "test", []string{"sh", "-c", "exit 9"})
  110. if err != nil {
  111. t.Fatal("unexpected error", err)
  112. }
  113. if exitCode != 9 {
  114. t.Fatal("expected exit code 9, got", exitCode)
  115. }
  116. })
  117. t.Run("signal", func(t *testing.T) {
  118. exitCode, err := Main(ioutil.Discard, "test", []string{"sh", "-c", "kill -9 $$"})
  119. if err != nil {
  120. t.Fatal("unexpected error", err)
  121. }
  122. if exitCode != 137 {
  123. t.Fatal("expected exit code 137, got", exitCode)
  124. }
  125. })
  126. t.Run("success", func(t *testing.T) {
  127. exitCode, err := Main(ioutil.Discard, "test", []string{"echo"})
  128. if err != nil {
  129. t.Fatal("unexpected error", err)
  130. }
  131. if exitCode != 0 {
  132. t.Fatal("expected exit code 0, got", exitCode)
  133. }
  134. })
  135. }