aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google
diff options
context:
space:
mode:
authorGravatar Yun Peng <pcloudy@google.com>2017-06-07 14:56:35 -0400
committerGravatar John Cater <jcater@google.com>2017-06-08 10:52:42 -0400
commit4ed112830894161614d9b33ee26b0f9f74eb05d9 (patch)
tree0e4840893d7364394b5c866da8b4198b1019f5f1 /src/test/java/com/google
parente763441482b98d4b4fc2d5e81e8939aed68a536c (diff)
Fix a bug in ShowIncludesFilter.java
Don't flush when buffer is a prefix of SHOW_INCLUDES_PREFIX or sourceFileName eg. Since we are looking for a line starting with "Note: including file:", when buffer equals "Note: i", when should not flush the buffer, but wait until the whole line is read or we are sure they won't match. Also, we need to reset the buffer after write to out. Added test for ShowIncludesFilter.java Change-Id: I58ebd0bfd5a3e2c9973c8bd910e2e5b85a545c21 PiperOrigin-RevId: 158297616
Diffstat (limited to 'src/test/java/com/google')
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/cpp/ShowIncludesFilterTest.java108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/ShowIncludesFilterTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/ShowIncludesFilterTest.java
new file mode 100644
index 0000000000..6e897d002c
--- /dev/null
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/ShowIncludesFilterTest.java
@@ -0,0 +1,108 @@
+// Copyright 2014 The Bazel Authors. 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 com.google.devtools.build.lib.rules.cpp;
+
+import static com.google.common.truth.Truth.assertThat;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Test for {@link ShowIncludesFilter}. */
+@RunWith(JUnit4.class)
+public class ShowIncludesFilterTest {
+
+ private ShowIncludesFilter showIncludesFilter;
+ private ByteArrayOutputStream output;
+ private FilterOutputStream filterOutputStream;
+
+ @Before
+ public void setUpOutputStreams() {
+ showIncludesFilter = new ShowIncludesFilter("foo.cpp");
+ output = new ByteArrayOutputStream();
+ filterOutputStream = showIncludesFilter.getFilteredOutputStream(output);
+ }
+
+ private byte[] getBytes(String str) {
+ return str.getBytes(UTF_8);
+ }
+
+ @Test
+ public void testNotMatch() throws IOException {
+ // Normal output message with newline
+ filterOutputStream.write(getBytes("I am compiling\n"));
+ assertThat(output.toString()).isEqualTo("I am compiling\n");
+ }
+
+ @Test
+ public void testNotMatchThenFlushing() throws IOException {
+ // Normal output message without newline
+ filterOutputStream.write(getBytes("Still compiling"));
+ assertThat(output.toString()).isEmpty();
+ filterOutputStream.flush();
+ // flush to output should succeed
+ assertThat(output.toString()).isEqualTo("Still compiling");
+ }
+
+ @Test
+ public void testMatchPartOfNotePrefix() throws IOException {
+ // Prefix of "Note: including file:"
+ filterOutputStream.write(getBytes("Note: "));
+ filterOutputStream.flush();
+ // flush to output shouldn't work, because there's still a chance to match.
+ assertThat(output.toString()).isEmpty();
+ // "Note: other info" doesn't match "Note: including file:", it's ok to flush.
+ filterOutputStream.write(getBytes("other info"));
+ filterOutputStream.flush();
+ assertThat(output.toString()).isEqualTo("Note: other info");
+ }
+
+ @Test
+ public void testMatchAllOfNotePrefix() throws IOException {
+ // "Note: including file:" is the prefix
+ filterOutputStream.write(getBytes("Note: including file: bar.h"));
+ filterOutputStream.flush();
+ // flush to output should not work, waiting for newline
+ assertThat(output.toString()).isEmpty();
+ filterOutputStream.write(getBytes("\n"));
+ // It's a match, output should be filtered, dependency on bar.h should be found.
+ assertThat(output.toString()).isEmpty();
+ assertThat(showIncludesFilter.getDependencies()).contains("bar.h");
+ }
+
+ @Test
+ public void testMatchSourceFileName() throws IOException {
+ filterOutputStream.write(getBytes("foo.cpp\n"));
+ // It's a match, output should be filtered, no dependency found.
+ assertThat(output.toString()).isEmpty();
+ assertThat(showIncludesFilter.getDependencies()).isEmpty();
+ }
+
+ @Test
+ public void testMatchPartOfSourceFileName() throws IOException {
+ filterOutputStream.write(getBytes("foo"));
+ filterOutputStream.flush();
+ assertThat(output.toString()).isEmpty();
+
+ filterOutputStream.write(getBytes(".h"));
+ filterOutputStream.flush();
+ assertThat(output.toString()).isEqualTo("foo.h");
+ }
+}