-
Notifications
You must be signed in to change notification settings - Fork 288
Check Workspace permission before showing source code viewer #3244
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad75768
Add permissions for source code viewer
akash-manna-sky c939fba
Remove PermissionDeniedViewModel and related resources; update Detail…
akash-manna-sky a02f92e
Refactor DetailFactoryTest for improved readability by adjusting form…
akash-manna-sky 7160121
Add integration tests for DetailFactory source file reading functiona…
akash-manna-sky 498937c
removed JenkinsFacade mock and simplify createDetails method
akash-manna-sky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...in/src/test/java/io/jenkins/plugins/analysis/core/model/DetailFactorySourceFileITest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,104 @@ | ||||||||||
| package io.jenkins.plugins.analysis.core.model; | ||||||||||
|
|
||||||||||
| import org.junit.jupiter.api.Test; | ||||||||||
|
|
||||||||||
| import edu.hm.hafner.analysis.IssueBuilder; | ||||||||||
| import edu.hm.hafner.analysis.Report; | ||||||||||
|
|
||||||||||
| import java.io.IOException; | ||||||||||
| import java.io.StringReader; | ||||||||||
| import java.nio.charset.Charset; | ||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||
|
|
||||||||||
| import hudson.model.Run; | ||||||||||
|
|
||||||||||
| import io.jenkins.plugins.analysis.core.testutil.IntegrationTestWithJenkinsPerSuite; | ||||||||||
| import io.jenkins.plugins.analysis.core.util.BuildFolderFacade; | ||||||||||
| import io.jenkins.plugins.prism.SourceCodeViewModel; | ||||||||||
| import io.jenkins.plugins.util.JenkinsFacade; | ||||||||||
|
|
||||||||||
| import static io.jenkins.plugins.analysis.core.testutil.Assertions.*; | ||||||||||
| import static org.mockito.ArgumentMatchers.*; | ||||||||||
| import static org.mockito.Mockito.*; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Integration tests for {@link DetailFactory} source file reading functionality. | ||||||||||
| * These tests require a Jenkins instance because SourceCodeViewModel.create() checks permissions. | ||||||||||
| * | ||||||||||
| * @author Akash Manna | ||||||||||
| */ | ||||||||||
| class DetailFactorySourceFileITest extends IntegrationTestWithJenkinsPerSuite { | ||||||||||
| private static final Charset ENCODING = StandardCharsets.UTF_8; | ||||||||||
| private static final String AFFECTED_FILE_CONTENT = "public class Test { }"; | ||||||||||
| private static final Report NEW_ISSUES = new Report(); | ||||||||||
| private static final Report OUTSTANDING_ISSUES = new Report(); | ||||||||||
| private static final Report FIXED_ISSUES = new Report(); | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Checks that the error message is shown if an affected file could not be read. | ||||||||||
| */ | ||||||||||
| @Test | ||||||||||
| void shouldShowExceptionMessageIfAffectedFileIsNotReadable() throws IOException { | ||||||||||
| JenkinsFacade jenkins = mock(JenkinsFacade.class); | ||||||||||
| BuildFolderFacade buildFolder = mock(BuildFolderFacade.class); | ||||||||||
| when(buildFolder.readFile(any(), anyString(), any())).thenThrow(new IOException("file error")); | ||||||||||
|
|
||||||||||
| var details = createDetails(jenkins, buildFolder, "a-file"); | ||||||||||
|
|
||||||||||
| assertThat(details).isInstanceOfSatisfying(SourceCodeViewModel.class, | ||||||||||
| s -> assertThat(s.getSourceCode()).contains("IOException: file error")); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Checks that a link to a source returns a SourceDetail-View. | ||||||||||
| */ | ||||||||||
| @Test | ||||||||||
| void shouldReturnSourceDetailWhenCalledWithSourceLinkAndIssueNotInConsoleLog() throws IOException { | ||||||||||
| JenkinsFacade jenkins = mock(JenkinsFacade.class); | ||||||||||
| BuildFolderFacade buildFolder = mock(BuildFolderFacade.class); | ||||||||||
| when(buildFolder.readFile(any(), anyString(), any())).thenReturn(new StringReader(AFFECTED_FILE_CONTENT)); | ||||||||||
|
|
||||||||||
| var details = createDetails(jenkins, buildFolder, "a-file"); | ||||||||||
|
|
||||||||||
| assertThat(details).isInstanceOfSatisfying(SourceCodeViewModel.class, | ||||||||||
| s -> assertThat(s.getSourceCode()).contains(AFFECTED_FILE_CONTENT)); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private Object createDetails(final JenkinsFacade jenkins, final BuildFolderFacade buildFolder, | ||||||||||
| final String fileName) { | ||||||||||
| try (var issueBuilder = new IssueBuilder()) { | ||||||||||
| var detailFactory = new DetailFactory(jenkins, buildFolder); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can use |
||||||||||
|
|
||||||||||
| issueBuilder.setFileName(fileName); | ||||||||||
| var issue = issueBuilder.build(); | ||||||||||
|
|
||||||||||
| var report = new Report(); | ||||||||||
| report.add(issue); | ||||||||||
|
|
||||||||||
| var project = createFreeStyleProject(); | ||||||||||
| buildSuccessfully(project); | ||||||||||
| Run<?, ?> run = project.getLastBuild(); | ||||||||||
|
|
||||||||||
| return detailFactory.createTrendDetails("source." + issue.getId().toString(), | ||||||||||
| run, createAnalysisResult(), report, NEW_ISSUES, OUTSTANDING_ISSUES, FIXED_ISSUES, ENCODING, | ||||||||||
| createParent()); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private AnalysisResult createAnalysisResult() { | ||||||||||
| AnalysisResult result = mock(AnalysisResult.class); | ||||||||||
| when(result.getErrorMessages()).thenReturn(org.eclipse.collections.impl.factory.Lists.immutable.empty()); | ||||||||||
| when(result.getInfoMessages()).thenReturn(org.eclipse.collections.impl.factory.Lists.immutable.empty()); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| return result; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| private IssuesDetail createParent() { | ||||||||||
| IssuesDetail parent = mock(IssuesDetail.class); | ||||||||||
| StaticAnalysisLabelProvider labelProvider = mock(StaticAnalysisLabelProvider.class); | ||||||||||
| when(labelProvider.getName()).thenReturn("Test"); | ||||||||||
| when(labelProvider.getSmallIconUrl()).thenReturn("/icon"); | ||||||||||
| when(labelProvider.getLargeIconUrl()).thenReturn("/icon"); | ||||||||||
| when(parent.getLabelProvider()).thenReturn(labelProvider); | ||||||||||
| return parent; | ||||||||||
| } | ||||||||||
| } | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.