From 2f7c856c0f66abcd5a87fbeac634bd91b56babdc Mon Sep 17 00:00:00 2001 From: Stefan Spieker Date: Sun, 15 Dec 2024 14:08:54 +0100 Subject: [PATCH] add section about operating system specific tests --- content/doc/developer/testing/index.adoc | 42 ++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/content/doc/developer/testing/index.adoc b/content/doc/developer/testing/index.adoc index 323d21ea0e7e..710d1bc2081c 100644 --- a/content/doc/developer/testing/index.adoc +++ b/content/doc/developer/testing/index.adoc @@ -246,6 +246,48 @@ To recreate the same configuration from a test method, you can do the following: In order to test parts of your plugin, you may want certain files to exist in the build workspace, or that Jenkins is configured in a certain way. This section covers various ways to achieve this using the Jenkins Test Harness. +=== Operating System Awareness + +Sometimes it ios necessary to adjust the testcase depending on the operating system. +This can be the choice of Batch or Shell commands. +The following example shows how to make use of `Functions.isWindows()` to create different code depending on the operating system: + +[source,java] +---- +import hudson.Functions; +import hudson.model.FreeStyleProject; +import hudson.tasks.BatchFile; +import hudson.tasks.Shell; +import org.jvnet.hudson.test.JenkinsRule; +import org.junit.Rule; +import org.junit.Test; + +public class MyPluginTest { + + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void testOnAllSystems() throws Exception { + FreeStyleProject project = j.createFreeStyleProject(); + if(Functions.isWindows()) { + project.getBuildersList().add(new BatchFile("echo ahoy > log.log")); + } else { + project.getBuildersList().add(new Shell("echo ahoy > log.log")); + } + j.buildAndAssertSuccess(project); + } +} +---- + +If you just want to disable a test on Windows systems you can make use of `assumeFalse()` in combination with `Functions.isWindows()`: + +`assumeFalse("TODO: Implement this test on Windows", Functions.isWindows());` + +The following Test will only be executed if it is not executed under windows. + +Be aware, that in general, we encourage to also test functionality under windows! + ==== Customizing the Build Workspace