Skip to content

Commit

Permalink
Adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avimanyum committed Feb 8, 2024
1 parent e1c9a83 commit ac16208
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void prepareToCreate(final Namespace ns,
* @param fork A GitHubContentToProcess object that contains the fork repository that is under process
* @return true if the file is found in the path specified and is not disabled, false otherwise
*/
private boolean isRenovateEnabled(List<String> filePaths, GitHubContentToProcess fork) throws IOException {
public boolean isRenovateEnabled(List<String> filePaths, GitHubContentToProcess fork) throws IOException {
for (String filePath : filePaths) {
try {
GHContent fileContent = fork.getParent().getFileContent(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.sourceforge.argparse4j.inf.*;
import org.kohsuke.github.*;
import org.mockito.*;
import org.testng.*;
import org.testng.annotations.*;

import java.io.*;
Expand Down Expand Up @@ -157,4 +158,70 @@ public void testPullRequestsPrepareSkipsSendingPRIfRepoOnboardedToRenovate() thr
eq(gitHubContentToProcess), anyList(), eq(gitForkBranch),
eq(rateLimiter));
}

@Test
public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileNotFound() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
GHRepository ghRepository = mock(GHRepository.class);
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
when(ghRepository.getFileContent(anyString())).thenThrow(new FileNotFoundException());
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}

@Test
public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundButIsDisabled() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
GHRepository ghRepository = mock(GHRepository.class);
GHContent content = mock(GHContent.class);
InputStream inputStream = new ByteArrayInputStream("{enabled:false}".getBytes());
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
when(ghRepository.getFileContent(anyString())).thenReturn(content);
when(content.read()).thenReturn(inputStream);
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}

@Test
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundButEnabledKeyNotFound() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
GHRepository ghRepository = mock(GHRepository.class);
GHContent content = mock(GHContent.class);
InputStream inputStream = new ByteArrayInputStream("{someKey:someValue}".getBytes());
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
when(ghRepository.getFileContent(anyString())).thenReturn(content);
when(content.read()).thenReturn(inputStream);
Assert.assertTrue(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}

@Test
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
GHRepository ghRepository = mock(GHRepository.class);
GHContent content = mock(GHContent.class);
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
when(ghRepository.getFileContent(anyString())).thenReturn(content);
when(content.read()).thenThrow(new IOException());
Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}

@Test
public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndEnabledKeySetToTrue() throws IOException {
PullRequests pullRequests = new PullRequests();
List<String> filePaths = Collections.singletonList("renovate.json");
GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class);
GHRepository ghRepository = mock(GHRepository.class);
GHContent content = mock(GHContent.class);
InputStream inputStream = new ByteArrayInputStream("{enabled:true}".getBytes());
when(gitHubContentToProcess.getParent()).thenReturn(ghRepository);
when(ghRepository.getFileContent(anyString())).thenReturn(content);
when(content.read()).thenReturn(inputStream);
Assert.assertTrue(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess));
}
}

0 comments on commit ac16208

Please sign in to comment.