Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation about FilePath and removed wip status #7783

Merged
merged 1 commit into from
Dec 26, 2024
Merged
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
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
Loading