DockerfileGitHubUtil { protected boolean rewriteDockerfile(String img, String tag, BufferedReader reader, StringBuilder strB) throws IOException { String line; boolean modified = false; while ( (line = reader.readLine()) != null ) { modified = changeIfDockerfileBaseImageLine(img, tag, strB, line) || modified; } return modified; } DockerfileGitHubUtil(GitHubUtil gitHubUtil); GHRepository getOrCreateFork(GHRepository parent); GHMyself getMyself(); GHRepository getRepo(String repoName); PagedSearchIterable<GHContent> findFilesWithImage(String query, String org); List<GHRepository> getGHRepositories(Multimap<String, String> pathToDockerfileInParentRepo,
                                                            GHMyself currentUser); void modifyAllOnGithub(GHRepository repo, String branch,
                                  String img, String tag); GHContent tryRetrievingContent(GHRepository repo, String path, String branch); void modifyOnGithub(GHContent content,
                               String branch, String img, String tag, String customMessage); GitHubJsonStore getGitHubJsonStore(String store); void createPullReq(GHRepository origRepo,
                              String branch, GHRepository forkRepo,
                              PullRequestInfo pullRequestInfo); Optional<GHPullRequest> getPullRequestForImageBranch(GHRepository repository, GitForkBranch gitForkBranch); void createOrUpdateForkBranchToParentDefault(GHRepository parent, GHRepository fork, GitForkBranch gitForkBranch); boolean thisUserIsOwner(GHRepository repo); Optional<PagedSearchIterable<GHContent>> getGHContents(String org, String img);  }
@Test public void testFindImagesAndFix_doNotDeleteOtherLines() throws Exception { gitHubUtil = mock(GitHubUtil.class); BufferedReader reader = mock(BufferedReader.class); GHContent content = mock(GHContent.class); when(content.getPath()).thenReturn("path"); when(content.update(anyString(), anyString(), anyString())).thenReturn(null); when(reader.readLine()).thenReturn("hello", "FROM image:tag", "this is a test", "", "", "", "world", null); dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil); StringBuilder strB = new StringBuilder(); dockerfileGitHubUtil.rewriteDockerfile("image", "newtag", reader, strB); assertEquals(strB.toString(), "hello\nFROM image:newtag\nthis is a test\n\n\n\nworld\n"); }
@Test(dataProvider = "postTagData") public void testFindImagesAndFix_doNotDeletePostTagData(String postTagData, String updatedTag, String expectedReplacedData) throws Exception { gitHubUtil = mock(GitHubUtil.class); BufferedReader reader = mock(BufferedReader.class); GHContent content = mock(GHContent.class); when(content.getPath()).thenReturn("path"); when(content.update(anyString(), anyString(), anyString())).thenReturn(null); when(reader.readLine()).thenReturn("hello", "FROM image" + postTagData, "this is a test", null); dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil); StringBuilder strB = new StringBuilder(); boolean modified = dockerfileGitHubUtil.rewriteDockerfile("image", updatedTag, reader, strB); assertTrue(modified, "Expect the dockerfile to have been modified"); assertEquals(strB.toString(), String.format("hello\n%s\nthis is a test\n", expectedReplacedData)); }
@Test public void testFindImagesAndFix_notModifiedPostData() throws Exception { gitHubUtil = mock(GitHubUtil.class); BufferedReader reader = mock(BufferedReader.class); GHContent content = mock(GHContent.class); when(content.getPath()).thenReturn("path"); when(content.update(anyString(), anyString(), anyString())).thenReturn(null); when(reader.readLine()).thenReturn("hello", "FROM image:tag as builder", "this is a test", null); dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil); StringBuilder strB = new StringBuilder(); boolean modified = dockerfileGitHubUtil.rewriteDockerfile("image", "tag", reader, strB); assertFalse(modified, "Expected the dockerfile to not have changed."); }