|
| 1 | +package io.jenkins.plugins.forensics.git.util; |
| 2 | + |
| 3 | +import org.assertj.core.util.Lists; |
| 4 | +import org.eclipse.jgit.lib.ObjectId; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import edu.hm.hafner.util.FilteredLog; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +import org.jenkinsci.plugins.gitclient.GitClient; |
| 13 | +import hudson.EnvVars; |
| 14 | +import hudson.FilePath; |
| 15 | +import hudson.model.Run; |
| 16 | +import hudson.model.Saveable; |
| 17 | +import hudson.model.TaskListener; |
| 18 | +import hudson.plugins.git.GitSCM; |
| 19 | +import hudson.plugins.git.extensions.impl.CloneOption; |
| 20 | +import hudson.scm.NullSCM; |
| 21 | +import hudson.util.DescribableList; |
| 22 | + |
| 23 | +import static io.jenkins.plugins.forensics.assertions.Assertions.*; |
| 24 | +import static org.mockito.Mockito.*; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests the class {@link GitRepositoryValidator}. |
| 28 | + * |
| 29 | + * @author Akash Manna |
| 30 | + */ |
| 31 | +class GitRepositoryValidatorTest { |
| 32 | + private static final TaskListener NULL_LISTENER = TaskListener.NULL; |
| 33 | + |
| 34 | + @Test |
| 35 | + void isGitRepositoryShouldReturnFalseForNonGitScm() { |
| 36 | + var logger = createLogger(); |
| 37 | + var validator = new GitRepositoryValidator(new NullSCM(), null, createWorkTree(), NULL_LISTENER, logger); |
| 38 | + |
| 39 | + assertThat(validator.isGitRepository()).isFalse(); |
| 40 | + assertThat(logger.getInfoMessages()).contains("SCM 'hudson.scm.NullSCM' is not of type GitSCM"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void isGitRepositoryShouldReturnTrueForNonShallowGit() throws IOException, InterruptedException { |
| 45 | + GitSCM gitSCM = createNonShallowGitScm(); |
| 46 | + Run<?, ?> run = mock(Run.class); |
| 47 | + var envVars = new EnvVars(); |
| 48 | + when(run.getEnvironment(NULL_LISTENER)).thenReturn(envVars); |
| 49 | + var workspace = createWorkTree(); |
| 50 | + GitClient gitClient = mock(GitClient.class); |
| 51 | + when(gitSCM.createClient(NULL_LISTENER, envVars, run, workspace)).thenReturn(gitClient); |
| 52 | + when(gitClient.revParse(anyString())).thenReturn(mock(ObjectId.class)); |
| 53 | + |
| 54 | + var logger = createLogger(); |
| 55 | + var validator = new GitRepositoryValidator(gitSCM, run, workspace, NULL_LISTENER, logger); |
| 56 | + |
| 57 | + assertThat(validator.isGitRepository()).isTrue(); |
| 58 | + assertThat(logger.getInfoMessages()).doesNotContain(GitRepositoryValidator.INFO_SHALLOW_CLONE); |
| 59 | + assertThat(logger.getInfoMessages()).doesNotContain(GitRepositoryValidator.INFO_SHALLOW_CLONE_COMMIT_RECORDING); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void isGitRepositoryShouldReturnTrueForShallowClone() throws IOException, InterruptedException { |
| 64 | + GitSCM gitSCM = createShallowGitScm(); |
| 65 | + Run<?, ?> run = mock(Run.class); |
| 66 | + var envVars = new EnvVars(); |
| 67 | + when(run.getEnvironment(NULL_LISTENER)).thenReturn(envVars); |
| 68 | + var workspace = createWorkTree(); |
| 69 | + GitClient gitClient = mock(GitClient.class); |
| 70 | + when(gitSCM.createClient(NULL_LISTENER, envVars, run, workspace)).thenReturn(gitClient); |
| 71 | + when(gitClient.revParse(anyString())).thenReturn(mock(ObjectId.class)); |
| 72 | + |
| 73 | + var logger = createLogger(); |
| 74 | + var validator = new GitRepositoryValidator(gitSCM, run, workspace, NULL_LISTENER, logger); |
| 75 | + |
| 76 | + assertThat(validator.isGitRepository()).isTrue(); |
| 77 | + assertThat(logger.getInfoMessages()).contains(GitRepositoryValidator.INFO_SHALLOW_CLONE_COMMIT_RECORDING); |
| 78 | + assertThat(logger.getInfoMessages()).doesNotContain(GitRepositoryValidator.INFO_SHALLOW_CLONE); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void isFullGitRepositoryShouldReturnFalseForNonGitScm() { |
| 83 | + var logger = createLogger(); |
| 84 | + var validator = new GitRepositoryValidator(new NullSCM(), null, createWorkTree(), NULL_LISTENER, logger); |
| 85 | + |
| 86 | + assertThat(validator.isFullGitRepository()).isFalse(); |
| 87 | + assertThat(logger.getInfoMessages()).contains("SCM 'hudson.scm.NullSCM' is not of type GitSCM"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void isFullGitRepositoryShouldReturnTrueForNonShallowGit() throws IOException, InterruptedException { |
| 92 | + GitSCM gitSCM = createNonShallowGitScm(); |
| 93 | + Run<?, ?> run = mock(Run.class); |
| 94 | + var envVars = new EnvVars(); |
| 95 | + when(run.getEnvironment(NULL_LISTENER)).thenReturn(envVars); |
| 96 | + var workspace = createWorkTree(); |
| 97 | + GitClient gitClient = mock(GitClient.class); |
| 98 | + when(gitSCM.createClient(NULL_LISTENER, envVars, run, workspace)).thenReturn(gitClient); |
| 99 | + when(gitClient.revParse(anyString())).thenReturn(mock(ObjectId.class)); |
| 100 | + |
| 101 | + var logger = createLogger(); |
| 102 | + var validator = new GitRepositoryValidator(gitSCM, run, workspace, NULL_LISTENER, logger); |
| 103 | + |
| 104 | + assertThat(validator.isFullGitRepository()).isTrue(); |
| 105 | + assertThat(logger.getInfoMessages()).doesNotContain(GitRepositoryValidator.INFO_SHALLOW_CLONE); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void isFullGitRepositoryShouldReturnFalseForShallowClone() { |
| 110 | + CloneOption shallowOption = mock(CloneOption.class); |
| 111 | + when(shallowOption.isShallow()).thenReturn(true); |
| 112 | + |
| 113 | + GitSCM gitSCM = mock(GitSCM.class); |
| 114 | + when(gitSCM.getExtensions()).thenReturn(new DescribableList<>(Saveable.NOOP, Lists.list(shallowOption))); |
| 115 | + |
| 116 | + var logger = createLogger(); |
| 117 | + var validator = new GitRepositoryValidator(gitSCM, mock(Run.class), createWorkTree(), NULL_LISTENER, logger); |
| 118 | + |
| 119 | + assertThat(validator.isFullGitRepository()).isFalse(); |
| 120 | + assertThat(logger.getInfoMessages()).contains(GitRepositoryValidator.INFO_SHALLOW_CLONE); |
| 121 | + assertThat(logger.getInfoMessages()).doesNotContain(GitRepositoryValidator.INFO_SHALLOW_CLONE_COMMIT_RECORDING); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void isShallowCloneShouldReturnFalseForNonGitScm() { |
| 126 | + var validator = new GitRepositoryValidator(new NullSCM(), null, createWorkTree(), NULL_LISTENER, createLogger()); |
| 127 | + |
| 128 | + assertThat(validator.isShallowClone()).isFalse(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void isShallowCloneShouldReturnFalseForNonShallowGit() { |
| 133 | + var validator = new GitRepositoryValidator(createNonShallowGitScm(), null, createWorkTree(), NULL_LISTENER, createLogger()); |
| 134 | + |
| 135 | + assertThat(validator.isShallowClone()).isFalse(); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void isShallowCloneShouldReturnTrueForShallowGit() { |
| 140 | + var validator = new GitRepositoryValidator(createShallowGitScm(), null, createWorkTree(), NULL_LISTENER, createLogger()); |
| 141 | + |
| 142 | + assertThat(validator.isShallowClone()).isTrue(); |
| 143 | + } |
| 144 | + |
| 145 | + private GitSCM createNonShallowGitScm() { |
| 146 | + GitSCM git = mock(GitSCM.class); |
| 147 | + when(git.getExtensions()).thenReturn(new DescribableList<>(Saveable.NOOP)); |
| 148 | + return git; |
| 149 | + } |
| 150 | + |
| 151 | + private GitSCM createShallowGitScm() { |
| 152 | + CloneOption shallowOption = mock(CloneOption.class); |
| 153 | + when(shallowOption.isShallow()).thenReturn(true); |
| 154 | + |
| 155 | + GitSCM git = mock(GitSCM.class); |
| 156 | + when(git.getExtensions()).thenReturn(new DescribableList<>(Saveable.NOOP, Lists.list(shallowOption))); |
| 157 | + return git; |
| 158 | + } |
| 159 | + |
| 160 | + private FilePath createWorkTree() { |
| 161 | + File mock = mock(File.class); |
| 162 | + when(mock.getPath()).thenReturn("/"); |
| 163 | + return new FilePath(mock); |
| 164 | + } |
| 165 | + |
| 166 | + private FilteredLog createLogger() { |
| 167 | + return new FilteredLog("errors"); |
| 168 | + } |
| 169 | +} |
0 commit comments