Skip to content

Commit 2390847

Browse files
authored
Refined go:embed handling for Go projects (#152)
* Fixed warning created by go:embed files. * go:embed test with a project resource were added. * User get notification in case go list command had errors. * Removed code warnings in files.
1 parent bc67740 commit 2390847

File tree

7 files changed

+516
-12
lines changed

7 files changed

+516
-12
lines changed

src/main/java/com/jfrog/ide/common/go/GoScanWorkspaceCreator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jfrog.ide.common.go;
22

3+
import org.apache.commons.lang3.StringUtils;
34
import org.apache.commons.lang3.exception.ExceptionUtils;
45
import org.jfrog.build.api.util.Log;
56
import org.jfrog.build.extractor.go.GoDriver;
@@ -26,6 +27,7 @@ public class GoScanWorkspaceCreator implements FileVisitor<Path> {
2627
private final Path sourceDir;
2728
private final Path targetDir;
2829
private final Log logger;
30+
private static final String[] EXCLUDED_DIRS = new String[]{".git", ".idea", ".vscode"};
2931

3032
public GoScanWorkspaceCreator(String executablePath, Path sourceDir, Path targetDir, Path goModAbsDir, Map<String, String> env, Log logger) {
3133
this.goDriver = new GoDriver(executablePath, env, goModAbsDir.toFile(), logger);
@@ -36,6 +38,10 @@ public GoScanWorkspaceCreator(String executablePath, Path sourceDir, Path target
3638

3739
@Override
3840
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
41+
// Skip excluded directories.
42+
if (StringUtils.equalsAny(dir.getFileName().toString(), EXCLUDED_DIRS)) {
43+
return FileVisitResult.SKIP_SUBTREE;
44+
}
3945
// Skip subdirectories with go.mod files.
4046
// These directories are different Go projects and their go files should not be in the root project.
4147
if (!sourceDir.equals(dir)) {
@@ -68,8 +74,13 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
6874
Path targetGoMod = targetDir.resolve(sourceDir.relativize(file));
6975
Files.copy(file, targetGoMod);
7076
goDriver.runCmd("run . -goModPath=" + targetGoMod.toAbsolutePath() + " -wd=" + sourceDir.toAbsolutePath(), true);
77+
return FileVisitResult.CONTINUE;
7178
}
7279
// Files other than go.mod and *.go files are not necessary to build the dependency tree of used Go packages.
80+
// Therefore, we just create an empty file with the same name so go:embed files won't cause a missing file error.
81+
if (!fileName.equals("go.sum")) {
82+
Files.createFile(targetDir.resolve(sourceDir.relativize(file)));
83+
}
7384
return FileVisitResult.CONTINUE;
7485
}
7586

src/main/java/com/jfrog/ide/common/go/GoTreeBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public DepTree createDependencyTree(GoDriver goDriver, Log logger, boolean verbo
6868
} catch (IOException e) {
6969
// Errors occurred during running "go list". Run again and this time ignore errors.
7070
usedModulesResults = goDriver.getUsedModules(false, true, dontBuildVcs);
71-
logger.warn("Errors occurred during building the Go dependency tree. The dependency tree may be incomplete:" +
71+
logger.error("Errors occurred during building the Go dependency tree. The dependency tree may be incomplete:" +
7272
System.lineSeparator() + ExceptionUtils.getRootCauseMessage(e));
7373
}
7474
if (usedModulesResults.getRes().isEmpty()) {

src/test/java/com/jfrog/ide/common/go/GoTreeBuilderTest.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,21 @@ public void testCreateDependencyTree5() {
127127
*/
128128
@Test
129129
public void testCreateDependencyTree6() {
130-
Map<String, Integer> expected = new HashMap<>();
131-
try {
132-
Path projectDir = GO_ROOT.resolve("project6");
133-
GoTreeBuilder treeBuilder = new GoTreeBuilder(null, projectDir, projectDir.resolve("go.mod").toString(), null, log);
134-
DepTree dt = treeBuilder.buildTree();
135-
fail("Expected an IOException being thrown");
136-
} catch (IOException e) {
137-
// This exception is expected being thrown
138-
} catch (Throwable e) {
139-
fail(ExceptionUtils.getStackTrace(e));
140-
}
130+
Path projectDir = GO_ROOT.resolve("project6");
131+
GoTreeBuilder treeBuilder = new GoTreeBuilder(null, projectDir, projectDir.resolve("go.mod").toString(), null, log);
132+
assertThrows(IOException.class, treeBuilder::buildTree);
133+
}
134+
135+
@Test
136+
public void testCreateDependencyTreeEmbedProject() throws IOException {
137+
Map<String, Integer> expected = new HashMap<>() {{
138+
put("github.com/jfrog/jfrog-cli-core:1.9.0", 10);
139+
put("github.com/jfrog/jfrog-client-go:0.26.1", 8);
140+
}};
141+
Path projectDir = GO_ROOT.resolve("embedProject");
142+
GoTreeBuilder treeBuilder = new GoTreeBuilder(null, projectDir, projectDir.resolve("go.mod").toString(), null, log);
143+
DepTree depTree = treeBuilder.buildTree();
144+
validateDependencyTreeResults(expected, depTree);
141145
}
142146

143147
private void validateDependencyTreeResults(Map<String, Integer> expected, DepTree actual) throws IOException {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module embedProject
2+
3+
go 1.16
4+
5+
require (
6+
github.com/jfrog/jfrog-cli-core v1.9.0
7+
github.com/jfrog/jfrog-client-go v0.26.1 // indirect
8+
)

0 commit comments

Comments
 (0)