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

Add Tests for FolderCreateOperation #110

Merged
merged 1 commit into from
Jul 27, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sp.sd.fileoperations;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class FolderCreateOperationTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Test
public void testDefaults() {
String defaultFolderPath = "defaultFolder";
FolderCreateOperation operation = new FolderCreateOperation(defaultFolderPath);

assertEquals(defaultFolderPath, operation.getFolderPath());
}

@Test
public void testRunFolderCreateOperation() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject("folderCreateTest");

String folderPath = "newFolder";
FolderCreateOperation operation = new FolderCreateOperation(folderPath);
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());

FilePath folder = new FilePath(jenkins.jenkins.getWorkspaceFor(project), folderPath);
assertTrue("The folder should have been created", folder.exists() && folder.isDirectory());
}

@Test
public void testRunFolderCreateOperationWithTokens() throws Exception {
EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
EnvVars envVars = prop.getEnvVars();
envVars.put("FOLDER_PATH", "tokenFolder");
jenkins.jenkins.getGlobalNodeProperties().add(prop);

FreeStyleProject project = jenkins.createFreeStyleProject("folderCreateTestWithTokens");

FolderCreateOperation operation = new FolderCreateOperation("$FOLDER_PATH");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

FreeStyleBuild build = project.scheduleBuild2(0).get();
assertEquals(Result.SUCCESS, build.getResult());

FilePath folder = new FilePath(jenkins.jenkins.getWorkspaceFor(project), "tokenFolder");
assertTrue("The folder should have been created", folder.exists() && folder.isDirectory());
}
}