aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Klaus Aehlig <aehlig@google.com>2016-08-02 10:06:07 +0000
committerGravatar Philipp Wollermann <philwo@google.com>2016-08-02 11:16:46 +0000
commite52813bff60b81188c5042e936bfb5249340cc58 (patch)
tree7abcf4f77b4a6881e4c59b2ec779908972e9d2e3 /src
parent8f08c63365d1e72d5f70285294d4df774d693a68 (diff)
ExperimentalStateTracker: make suffix gracefully handle negative length
When requested to produce a suffix of a string of a string of a given length, gracefully handle the case where the requested length is negative---simply return the empty string in this case. While there, mark this static method as such; also increase visibility to default as it is generally useful and should be tested as an interface of this class. -- Change-Id: I821966f7ba3828809bc6d000358803c131740ec9 Reviewed-on: https://bazel-review.googlesource.com/#/c/4223 MOS_MIGRATED_REVID=129080284
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/ExperimentalStateTracker.java6
-rw-r--r--src/test/java/com/google/devtools/build/lib/runtime/ExperimentalStateTrackerTest.java15
2 files changed, 15 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalStateTracker.java b/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalStateTracker.java
index a3f4bceecb..2fa11972ac 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalStateTracker.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalStateTracker.java
@@ -33,7 +33,6 @@ import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.util.io.AnsiTerminalWriter;
import com.google.devtools.build.lib.util.io.PositionAwareAnsiTerminalWriter;
import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
-
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
@@ -235,7 +234,10 @@ class ExperimentalStateTracker {
/**
* From a string, take a suffix of at most the given length.
*/
- private String suffix(String s, int len) {
+ static String suffix(String s, int len) {
+ if (len <= 0) {
+ return "";
+ }
int startPos = s.length() - len;
if (startPos <= 0) {
return s;
diff --git a/src/test/java/com/google/devtools/build/lib/runtime/ExperimentalStateTrackerTest.java b/src/test/java/com/google/devtools/build/lib/runtime/ExperimentalStateTrackerTest.java
index a416abfee0..a0f41d523d 100644
--- a/src/test/java/com/google/devtools/build/lib/runtime/ExperimentalStateTrackerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/runtime/ExperimentalStateTrackerTest.java
@@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.runtime;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
@@ -38,15 +39,13 @@ import com.google.devtools.build.lib.util.io.LoggingTerminalWriter;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
-
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
-import java.io.IOException;
-import java.util.concurrent.TimeUnit;
-
/**
* Tests {@link ExperimentalStateTracker}.
*/
@@ -562,4 +561,12 @@ public class ExperimentalStateTrackerTest extends FoundationTestCase {
"Progress bar should contain 'Other action', but was:\n" + output,
output.contains("Other action"));
}
+
+
+ @Test
+ public void testSuffix() throws Exception {
+ assertEquals("bar", ExperimentalStateTracker.suffix("foobar", 3));
+ assertEquals("", ExperimentalStateTracker.suffix("foo", -2));
+ assertEquals("foobar", ExperimentalStateTracker.suffix("foobar", 200));
+ }
}