Skip to content

Commit ea52030

Browse files
committed
Add MockOnlyRule to allow skipping specific tests based on downloader
1 parent 27a20e4 commit ea52030

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.schabi.newpipe;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Marker annotation to skip test if it not run with mocks.
11+
*
12+
* {@link MockOnlyRule}
13+
*/
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Target({ElementType.METHOD, ElementType.TYPE})
16+
@Inherited
17+
public @interface MockOnly {
18+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.schabi.newpipe;
2+
3+
import org.junit.Assume;
4+
import org.junit.rules.TestRule;
5+
import org.junit.runner.Description;
6+
import org.junit.runners.model.Statement;
7+
import org.schabi.newpipe.downloader.DownloaderType;
8+
9+
import javax.annotation.Nonnull;
10+
11+
/**
12+
* <p>
13+
* Checks if the system variable "downloader" is set to "REAL" and skips the tests if it is.
14+
* Otherwise execute the test.
15+
* </p>
16+
*
17+
* <p>
18+
* Use it by creating a public variable of this inside the test class and annotate it with
19+
* {@link org.junit.Rule}. Then annotate the tests to be skipped with {@link MockOnly}
20+
* </p>
21+
*
22+
* <p>
23+
* Allows skipping unreliable or time sensitive tests in CI pipeline.
24+
* </p>
25+
*/
26+
public class MockOnlyRule implements TestRule {
27+
28+
final String downloader = System.getProperty("downloader");
29+
30+
@Override
31+
@Nonnull
32+
public Statement apply(@Nonnull Statement base, @Nonnull Description description) {
33+
return new Statement() {
34+
@Override
35+
public void evaluate() throws Throwable {
36+
final boolean hasAnnotation = description.getAnnotation(MockOnly.class) == null;
37+
final boolean isMockDownloader = downloader == null ||
38+
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString());
39+
Assume.assumeTrue(
40+
"The test is not reliable against a website and thus skipped",
41+
hasAnnotation && isMockDownloader
42+
);
43+
44+
base.evaluate();
45+
}
46+
};
47+
}
48+
}

0 commit comments

Comments
 (0)