Skip to content

Commit

Permalink
DCA11Y-1145: fail quickly when nvm can't fails, e.g. no versioning fi…
Browse files Browse the repository at this point in the history
…le found
  • Loading branch information
flipatlas committed Sep 20, 2024
1 parent cbf1733 commit 0e2c515
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FNM_DIR=$HOME/.fnm
PATH=$FNM_DIR:$PATH
eval "$(fnm env --use-on-cd --shell bash)"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# invoker env variables are not loaded yet
export HOME="$(dirname "$0")"

export FNM_DIR="$HOME/.fnm";
mkdir "$FNM_DIR"

curl -fsSL https://fnm.vercel.app/install | bash -s -- --install-dir "$FNM_DIR" --skip-shell --force-install
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
invoker.buildResult=failure

invoker.environmentVariables.HOME=${basedir}/target/it/node-version-manager/fnm/with-no-version-file
invoker.environmentVariables.FNM_DIR=${basedir}/target/it/node-version-manager/fnm/with-no-version-file/.fnm
invoker.environmentVariables.XDG_DATA_HOME=${basedir}/target/it/node-version-manager/fnm/with-no-version-file

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "example",
"version": "0.0.1",
"dependencies": {
"classnames": "^2.3.2"
},
"scripts": {
"prebuild": "npm install"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.eirslett</groupId>
<artifactId>fnm-with-npm</artifactId>
<version>0</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
<version>@project.version@</version>

<configuration>
<workingDirectory>${basedir}</workingDirectory>
</configuration>

<executions>
<execution>
<id>install node</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm ci</id>
<goals>
<goal>npm</goal>
</goals>
<!-- Optional configuration which provides for running any npm command -->
<configuration>
<arguments>ci</arguments>
</configuration>
</execution>

</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// TODO share installation as test utils https://maven.apache.org/plugins/maven-invoker-plugin/integration-test-mojo.html#addTestClassPath
def p = "bash $basedir/install-fnm.sh".execute()
p.waitFor()
println p.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import org.codehaus.plexus.util.FileUtils

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'))
assert buildLog.contains('Installing node with FNM') : 'Node has been installed with a different version manager'
assert buildLog.contains('error: Can\'t find version in dotfiles. Please provide a version manually to the command.') : 'Node installation hasn\'t failed with a descriptive message'
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ShellExecutor {

Expand All @@ -26,21 +20,13 @@ public ShellExecutor(InstallConfig config) {
this.config = config;
}

public String execute(List<String> command) {
List<String> profiledShellCommand = getShellCommand(command);

ProcessExecutor executor = new ProcessExecutor(
config.getWorkingDirectory(),
Collections.emptyList(),
profiledShellCommand,
config.getPlatform(),
Collections.emptyMap());

public String executeAndCatchErrors(List<String> command) {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
List<String> profiledShellCommand = getShellCommand(command);

try {
int exitValue = executor.execute(logger, stdout, stderr);
int exitValue = execute(profiledShellCommand, stdout, stderr);
if (exitValue != 0) {
logger.debug("Command finished with error exit code {}, error output `{}`", exitValue, parseOutput(stderr));
}
Expand All @@ -53,6 +39,44 @@ public String execute(List<String> command) {
return output;
}

public String executeOrFail(List<String> command) {
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
List<String> profiledShellCommand = getShellCommand(command);

boolean hasExecutionFailed = false;
try {
int exitValue = execute(profiledShellCommand, stdout, stderr);
if (exitValue != 0) {
hasExecutionFailed = true;
}
} catch (ProcessExecutionException e) {
hasExecutionFailed = true;
}

if (hasExecutionFailed) {
String commandText = String.join(" ", profiledShellCommand);
throw new RuntimeException(String.format("Execution of `%s` has failed" +
"\nstdout: `%s`" +
"\nstderr: `%s`", commandText, parseOutput(stdout), parseOutput(stderr)));
} else {
String output = parseOutput(stdout);
logger.debug("Command output: `{}`", output);
return output;
}
}

private int execute(List<String> command, ByteArrayOutputStream stdout, ByteArrayOutputStream stderr) throws ProcessExecutionException {
ProcessExecutor executor = new ProcessExecutor(
config.getWorkingDirectory(),
Collections.emptyList(),
command,
config.getPlatform(),
Collections.emptyMap());

return executor.execute(logger, stdout, stderr);
}

private List<String> getShellCommand(List<String> command) {
List<String> profiledShellCommand = new ArrayList<>();
profiledShellCommand.add(getCurrentShell());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public AsdfClient(ShellExecutor shellExecutor) {

@Override
public boolean isInstalled() {
String version = shellExecutor.execute(Arrays.asList(
String version = shellExecutor.executeAndCatchErrors(Arrays.asList(
EXECUTABLE, "--version"
));

Expand All @@ -30,17 +30,17 @@ public boolean isInstalled() {
@Override
public void installNode() {

shellExecutor.execute(Arrays.asList(
shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "plugin", "add", "nodejs"
));
shellExecutor.execute(Arrays.asList(
shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "install", "nodejs"
));
}

@Override
public File getNodeExecutable() {
return new File(shellExecutor.execute(Arrays.asList(
return new File(shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "which", "node"
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public FnmClient(ShellExecutor shellExecutor) {

@Override
public boolean isInstalled() {
String version = cleanOutput(shellExecutor.execute(Arrays.asList(
String version = cleanOutput(shellExecutor.executeAndCatchErrors(Arrays.asList(
EXECUTABLE, "--version"
)));

Expand All @@ -30,15 +30,15 @@ public boolean isInstalled() {

@Override
public void installNode() {
shellExecutor.execute(Arrays.asList(
shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "install"
));
}

@Override
public File getNodeExecutable() {
String currentNodeVersion = cleanOutput(shellExecutor.execute(Arrays.asList(EXECUTABLE, "current")));
String fnmDir = cleanOutput(shellExecutor.execute(Arrays.asList("echo", "$FNM_DIR")));
String currentNodeVersion = cleanOutput(shellExecutor.executeOrFail(Arrays.asList(EXECUTABLE, "current")));
String fnmDir = cleanOutput(shellExecutor.executeOrFail(Arrays.asList("echo", "$FNM_DIR")));

return Paths.get(fnmDir, "node-versions", currentNodeVersion, "installation", "bin", "node").toFile();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;

public class MiseClient implements VersionManagerClient {
final Logger logger = LoggerFactory.getLogger(getClass());
Expand All @@ -21,7 +20,7 @@ public MiseClient(ShellExecutor shellExecutor) {

@Override
public boolean isInstalled() {
String version = shellExecutor.execute(Arrays.asList(
String version = shellExecutor.executeAndCatchErrors(Arrays.asList(
EXECUTABLE, "--version"
));

Expand All @@ -30,14 +29,14 @@ public boolean isInstalled() {

@Override
public void installNode() {
shellExecutor.execute(Arrays.asList(
shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "install", "node"
));
}

@Override
public File getNodeExecutable() {
return new File(shellExecutor.execute(Arrays.asList(
return new File(shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "which", "node"
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public NvmClient(ShellExecutor shellExecutor) {

@Override
public boolean isInstalled() {
String version = shellExecutor.execute(Arrays.asList(
String version = shellExecutor.executeAndCatchErrors(Arrays.asList(
EXECUTABLE, "--version"
));

Expand All @@ -28,14 +28,14 @@ public boolean isInstalled() {

@Override
public void installNode() {
shellExecutor.execute(Arrays.asList(
shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "install"
));
}

@Override
public File getNodeExecutable() {
return new File(shellExecutor.execute(Arrays.asList(
return new File(shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "which", "node"
)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public NvsClient(ShellExecutor shellExecutor) {

@Override
public boolean isInstalled() {
String version = shellExecutor.execute(Arrays.asList(
String version = shellExecutor.executeAndCatchErrors(Arrays.asList(
EXECUTABLE, "--version"
));

Expand All @@ -28,14 +28,14 @@ public boolean isInstalled() {

@Override
public void installNode() {
shellExecutor.execute(Arrays.asList(
shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "add"
));
}

@Override
public File getNodeExecutable() {
return new File(shellExecutor.execute(Arrays.asList(
return new File(shellExecutor.executeOrFail(Arrays.asList(
EXECUTABLE, "which", "node"
)));
}
Expand Down

0 comments on commit 0e2c515

Please sign in to comment.