Skip to content
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

#298: refactoring setting environment variables #1098

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/awstest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aws linux -i C:\projects\IDEasy\workspaces\main\IDEasy\cli\target\test-projects\aws\_ide\software\default\aws\aws\2.24.15 -b C:\projects\IDEasy\workspaces\main\IDEasy\cli\target\test-projects\aws\_ide\software\default\aws\aws\2.24.15
20 changes: 11 additions & 9 deletions cli/src/main/java/com/devonfw/tools/ide/tool/aws/Aws.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.nls.NlsBundle;
import com.devonfw.tools.ide.process.EnvironmentContext;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
import com.devonfw.tools.ide.tool.ToolInstallation;

/**
* {@link LocalToolCommandlet} for <a href="https://docs.aws.amazon.com/cli/">AWS CLI</a> (Amazon Web Services Command Line Interface).
Expand All @@ -32,15 +32,8 @@ public Aws(IdeContext context) {
public void postInstall() {

super.postInstall();
EnvironmentVariables variables = this.context.getVariables();
EnvironmentVariables typeVariables = variables.getByType(EnvironmentVariablesType.CONF);
Path awsConfigDir = this.context.getConfPath().resolve("aws");
this.context.getFileAccess().mkdirs(awsConfigDir);
Path awsConfigFile = awsConfigDir.resolve("config");
Path awsCredentialsFile = awsConfigDir.resolve("credentials");
typeVariables.set("AWS_CONFIG_FILE", awsConfigFile.toString(), true);
typeVariables.set("AWS_SHARED_CREDENTIALS_FILE", awsCredentialsFile.toString(), true);
typeVariables.save();
}

@Override
Expand Down Expand Up @@ -78,4 +71,13 @@ public void printHelp(NlsBundle bundle) {
this.context.info("To get detailed help about the usage of the AWS CLI, use \"aws help\"");
}

@Override
public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {

super.setEnvironment(environmentContext, toolInstallation, extraInstallation);
Path awsConfigDir = this.context.getConfPath().resolve("aws");
environmentContext.withEnvVar("AWS_CONFIG_FILE", awsConfigDir.resolve("config").toString());
environmentContext.withEnvVar("AWS_SHARED_CREDENTIALS_FILE", awsConfigDir.resolve("credentials").toString());
}

}
16 changes: 9 additions & 7 deletions cli/src/main/java/com/devonfw/tools/ide/tool/az/Azure.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.process.EnvironmentContext;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.tool.ToolInstallation;

/**
* {@link ToolCommandlet} for azure CLI (azure).
Expand All @@ -30,11 +30,6 @@ public Azure(IdeContext context) {
public void postInstall() {

super.postInstall();

EnvironmentVariables variables = this.context.getVariables();
EnvironmentVariables typeVariables = variables.getByType(EnvironmentVariablesType.CONF);
typeVariables.set("AZURE_CONFIG_DIR", this.context.getConfPath().resolve(".azure").toString(), true);
typeVariables.save();
this.context.getFileAccess().symlink(Path.of("wbin"), getToolPath().resolve("bin"));
}

Expand All @@ -43,4 +38,11 @@ public String getToolHelpArguments() {

return "-h";
}

@Override
public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean extraInstallation) {

super.setEnvironment(environmentContext, toolInstallation, extraInstallation);
environmentContext.withEnvVar("AZURE_CONFIG_DIR", this.context.getConfPath().resolve(".azure").toString());
}
}
13 changes: 0 additions & 13 deletions cli/src/main/java/com/devonfw/tools/ide/tool/graalvm/GraalVm.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.EnvironmentVariables;
import com.devonfw.tools.ide.environment.EnvironmentVariablesType;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
import com.devonfw.tools.ide.tool.ToolCommandlet;
import com.devonfw.tools.ide.tool.plugin.PluginBasedCommandlet;
import com.devonfw.tools.ide.version.VersionIdentifier;

/**
Expand Down Expand Up @@ -45,13 +41,4 @@ protected String getBinaryName() {
return "java";
}

@Override
public void postInstall() {

EnvironmentVariables envVars = this.context.getVariables().getByType(EnvironmentVariablesType.CONF);
envVars.set("GRAALVM_HOME", getToolPath().toString(), true);
envVars.save();
super.postInstall();
}

}
111 changes: 111 additions & 0 deletions cli/src/test/java/com/devonfw/tools/ide/tool/aws/AwsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.devonfw.tools.ide.tool.aws;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.devonfw.tools.ide.commandlet.EnvironmentCommandlet;
import com.devonfw.tools.ide.commandlet.HelpCommandlet;
import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogEntry;
import com.devonfw.tools.ide.nls.NlsBundle;
import com.devonfw.tools.ide.os.SystemInfo;
import com.devonfw.tools.ide.os.SystemInfoMock;

/**
* Integration test of {@link Aws}.
*/
public class AwsTest extends AbstractIdeContextTest {

private static final String PROJECT_AWS = "aws";
private final IdeTestContext context = newContext(PROJECT_AWS);

/**
* Tests if the {@link Aws} can be installed properly.
*
* @param os String of the OS to use.
*/
@ParameterizedTest
@ValueSource(strings = { "windows", "mac" })
public void testAwsInstall(String os) {

// arrange
SystemInfo systemInfo = SystemInfoMock.of(os);
context.setSystemInfo(systemInfo);
Aws awsCommandlet = new Aws(context);

// act
awsCommandlet.install();

// assert
checkInstallation(context);
assertThat(awsCommandlet.getName()).isEqualTo(PROJECT_AWS);
assertThat(awsCommandlet.getTags()).containsExactly(Tag.CLOUD);

//if tool already installed
awsCommandlet.install();
assertThat(context).logAtDebug().hasMessageContaining("Version 2.24.15 of tool aws is already installed");
}

/**
* Tests if the environment variables are correctly set after {@link Aws} is installed.
*
* @param os String of the OS to use.
*/
@ParameterizedTest
@ValueSource(strings = { "windows", "mac" })
public void testAwsSetEnvironment(String os) {

// arrange
SystemInfo systemInfo = SystemInfoMock.of(os);
context.setSystemInfo(systemInfo);
Aws awsCommandlet = new Aws(context);
EnvironmentCommandlet envCommandlet = new EnvironmentCommandlet(context);

// act
awsCommandlet.install();
envCommandlet.run();

// assert
if (os.equals("mac")) {
assertThat(context).log().hasEntries(
IdeLogEntry.ofProcessable("export AWS_CONFIG_FILE=\"" + context.getConfPath().resolve(PROJECT_AWS).resolve("config") + "\""), //
IdeLogEntry.ofProcessable("export AWS_SHARED_CREDENTIALS_FILE=\"" + context.getConfPath().resolve(PROJECT_AWS).resolve("credentials") + "\"")
);
} else {
assertThat(context).log().hasEntries(
IdeLogEntry.ofProcessable("AWS_CONFIG_FILE=" + context.getConfPath().resolve(PROJECT_AWS).resolve("config")), //
IdeLogEntry.ofProcessable("AWS_SHARED_CREDENTIALS_FILE=" + context.getConfPath().resolve(PROJECT_AWS).resolve("credentials"))
);
}

}

/**
* Tests if the output of {@link Aws#printHelp(NlsBundle)} is correct.
*/
@Test
public void testAwsPrintHelp() {

// arrange
HelpCommandlet helpCommandlet = new HelpCommandlet(context);
helpCommandlet.commandlet.setValueAsString(PROJECT_AWS, context);

// act
helpCommandlet.run();

// assert
assertThat(context).logAtInfo()
.hasMessage("To get detailed help about the usage of the AWS CLI, use \"aws help\"");
}

private void checkInstallation(IdeTestContext context) {

assertThat(context.getSoftwarePath().resolve("aws/.ide.software.version")).exists().hasContent("2.24.15");
assertThat(context).logAtSuccess().hasMessage("Successfully installed aws in version 2.24.15");
assertThat(context.getConfPath().resolve(PROJECT_AWS)).exists();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AWS_VERSION=2.24.15
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
cd "$(dirname "$0")"
echo $PWD
echo "aws linux $*"
echo "aws linux $*" > awstest

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
cd "$(dirname "$0")"
echo "aws mac $*" > awstest



Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
cd "$(dirname "$0")"
echo "aws windows $*" > awstest

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${testbaseurl}/download/aws/aws/2.24.15/aws-2.24.15.tgz