diff --git a/content/doc/developer/testing/index.adoc b/content/doc/developer/testing/index.adoc index b470fe9f4bb8..826e8ab3d6cb 100644 --- a/content/doc/developer/testing/index.adoc +++ b/content/doc/developer/testing/index.adoc @@ -1,7 +1,6 @@ --- title: Testing layout: developerchapter -wip: true references: - url: https://wiki.jenkins.io/display/JENKINS/Mocking+in+Unit+Tests title: "Wiki: Mocking in Unit Tests" @@ -357,7 +356,12 @@ public class PipelineWorkspaceExampleTest { <1> The path to the zip file is dynamic, so we pass it into the Pipeline definition. ===== Using `FilePath` -TODO: Expand this section, and explain the below example. + +In Jenkins plugin development, the link:https://javadoc.jenkins.io/hudson/FilePath.html[`FilePath`] class is a key utility used to interact with files and directories on the nodes where Jenkins jobs execute. +Unlike the basic `File`, it allows plugin developers to work seamlessly with files across controller and agent nodes, handling the distribution and access to remote resources. + +In test scenarios, you often need to set up a controlled environment with specific files or directories. +You can use `FilePath` to create them and use the method `copyFrom()` to place the data from FileItem into the file location specified by this FilePath object: [source,java] ---- @@ -366,6 +370,13 @@ FilePath report = workspace.child("target").child("lint-results.xml"); report.copyFrom(getClass().getResourceAsStream("lint-results_r20.xml")); ---- +If you want to create files or folder remotely on an agent node, the below example shows how to use it: +[source,java] +---- +FilePath remotePath = new FilePath(agentChannel, "/remote/path/file.txt"); +remotePath.write("Remote Content", "UTF-8"); +---- + ==== Customizing the `JENKINS_HOME` Directory If you want to adjust the `JENKINS_HOME` within a test, use the `JenkinsRule`-methods link:https://javadoc.jenkins.io/component/jenkins-test-harness/org/jvnet/hudson/test/JenkinsRule.html#withExistingHome(java.io.File)[`withExistingHome()`] or link:https://javadoc.jenkins.io/component/jenkins-test-harness/org/jvnet/hudson/test/JenkinsRule.html#withNewHome()[`withNewHome()`].