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 FolderDeleteOperation #112

Merged
merged 1 commit into from
Jul 28, 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,75 @@
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.assertFalse;

public class FolderDeleteOperationTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();

@Test
public void testDefaults() {
String folderPath = "folderToDelete";
FolderDeleteOperation operation = new FolderDeleteOperation(folderPath);

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

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

FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
FilePath folderToDelete = new FilePath(workspace, "folderToDelete");

folderToDelete.mkdirs();
FilePath fileInFolder = new FilePath(folderToDelete, "file.txt");
fileInFolder.write("Sample content", "UTF-8");

FolderDeleteOperation operation = new FolderDeleteOperation("folderToDelete");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

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

assertFalse("The folder should have been deleted", folderToDelete.exists());
}

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

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

FilePath workspace = jenkins.jenkins.getWorkspaceFor(project);
FilePath folderToDelete = new FilePath(workspace, "folderToDelete");

folderToDelete.mkdirs();
FilePath fileInFolder = new FilePath(folderToDelete, "file.txt");
fileInFolder.write("Sample content", "UTF-8");

FolderDeleteOperation operation = new FolderDeleteOperation("$FOLDER_TO_DELETE");
project.getBuildersList().add(new FileOperationsBuilder(List.of(operation)));

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

assertFalse("The folder should have been deleted", folderToDelete.exists());
}
}