Skip to content

Commit

Permalink
Added documentation about FilePath and removed wip status (#7783)
Browse files Browse the repository at this point in the history
added documentation about FilePath and removed wip status
  • Loading branch information
StefanSpieker authored Dec 26, 2024
1 parent 86a9cb6 commit e1097b7
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions content/doc/developer/testing/index.adoc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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]
----
Expand All @@ -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()`].
Expand Down

0 comments on commit e1097b7

Please sign in to comment.