Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions extractor/src/test/java/org/schabi/newpipe/MockOnly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.schabi.newpipe;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.annotation.Nonnull;

/**
* Marker annotation to skip test in certain cases.
*
* {@link MockOnlyRule}
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Inherited
public @interface MockOnly {

/**
* Explanation why this test should be skipped
*/
@Nonnull String reason();
}
51 changes: 51 additions & 0 deletions extractor/src/test/java/org/schabi/newpipe/MockOnlyRule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.schabi.newpipe;

import org.junit.Assume;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.schabi.newpipe.downloader.DownloaderType;

import javax.annotation.Nonnull;

/**
*
* <p>
* Allows skipping unreliable or time sensitive tests in CI pipeline.
* </p>
*
* <p>
* Use it by creating a public variable of this inside the test class and annotate it with
* {@link org.junit.Rule}. Then annotate the specific tests to be skipped with {@link MockOnly}
* </p>
*
* <p>
* It works by checking if the system variable "downloader" is set to "REAL" and skips the tests if it is.
* Otherwise it executes the test.
* </p>

*/
public class MockOnlyRule implements TestRule {

final String downloader = System.getProperty("downloader");

@Override
@Nonnull
public Statement apply(@Nonnull Statement base, @Nonnull Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
final MockOnly annotation = description.getAnnotation(MockOnly.class);
if (annotation != null) {
final boolean isMockDownloader = downloader == null ||
!downloader.equalsIgnoreCase(DownloaderType.REAL.toString());

Assume.assumeTrue("The test is not reliable against real website. Reason: "
+ annotation.reason(), isMockDownloader);
}

base.evaluate();
}
};
}
}