Skip to content

Commit

Permalink
Refined go:embed handling for Go projects (#152)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
noyshabtay authored Dec 3, 2023
1 parent bc67740 commit 2390847
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/jfrog/ide/common/go/GoScanWorkspaceCreator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jfrog.ide.common.go;

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/jfrog/ide/common/go/GoTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public DepTree createDependencyTree(GoDriver goDriver, Log logger, boolean verbo
} catch (IOException e) {
// Errors occurred during running "go list". Run again and this time ignore errors.
usedModulesResults = goDriver.getUsedModules(false, true, dontBuildVcs);
logger.warn("Errors occurred during building the Go dependency tree. The dependency tree may be incomplete:" +
logger.error("Errors occurred during building the Go dependency tree. The dependency tree may be incomplete:" +
System.lineSeparator() + ExceptionUtils.getRootCauseMessage(e));
}
if (usedModulesResults.getRes().isEmpty()) {
Expand Down
26 changes: 15 additions & 11 deletions src/test/java/com/jfrog/ide/common/go/GoTreeBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,21 @@ public void testCreateDependencyTree5() {
*/
@Test
public void testCreateDependencyTree6() {
Map<String, Integer> expected = new HashMap<>();
try {
Path projectDir = GO_ROOT.resolve("project6");
GoTreeBuilder treeBuilder = new GoTreeBuilder(null, projectDir, projectDir.resolve("go.mod").toString(), null, log);
DepTree dt = treeBuilder.buildTree();
fail("Expected an IOException being thrown");
} catch (IOException e) {
// This exception is expected being thrown
} catch (Throwable e) {
fail(ExceptionUtils.getStackTrace(e));
}
Path projectDir = GO_ROOT.resolve("project6");
GoTreeBuilder treeBuilder = new GoTreeBuilder(null, projectDir, projectDir.resolve("go.mod").toString(), null, log);
assertThrows(IOException.class, treeBuilder::buildTree);
}

@Test
public void testCreateDependencyTreeEmbedProject() throws IOException {
Map<String, Integer> expected = new HashMap<>() {{
put("github.com/jfrog/jfrog-cli-core:1.9.0", 10);
put("github.com/jfrog/jfrog-client-go:0.26.1", 8);
}};
Path projectDir = GO_ROOT.resolve("embedProject");
GoTreeBuilder treeBuilder = new GoTreeBuilder(null, projectDir, projectDir.resolve("go.mod").toString(), null, log);
DepTree depTree = treeBuilder.buildTree();
validateDependencyTreeResults(expected, depTree);
}

private void validateDependencyTreeResults(Map<String, Integer> expected, DepTree actual) throws IOException {
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/go/embedProject/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module embedProject

go 1.16

require (
github.com/jfrog/jfrog-cli-core v1.9.0
github.com/jfrog/jfrog-client-go v0.26.1 // indirect
)
Loading

0 comments on commit 2390847

Please sign in to comment.