Skip to content

Commit

Permalink
first unittest npm
Browse files Browse the repository at this point in the history
  • Loading branch information
ndemirca committed Feb 29, 2024
1 parent a59bb6a commit f646b4d
Show file tree
Hide file tree
Showing 54 changed files with 200 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.devonfw.tools.ide.commandlet.FileExtractor;

enum ExtractorFileType {
ZIP, JAR, DMG, MSI, PKG
ZIP, JAR, DMG, MSI, PKG, TAR, GZ, TGZ
}
30 changes: 16 additions & 14 deletions cli/src/main/java/com/devonfw/tools/ide/tool/npm/Npm.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ public void postInstall() {

FileAccess fileAccess = context.getFileAccess();
Path nodeHomePath = this.context.getSoftwarePath().resolve("node/");
Path npmBinBath = nodeHomePath.resolve("node_modules/npm/bin/");
String npm = "npm";
String npx = "npx";
String cmd = ".cmd";

fileAccess.delete(nodeHomePath.resolve(npm));
fileAccess.delete(nodeHomePath.resolve(npm + cmd));
fileAccess.delete(nodeHomePath.resolve(npx));
fileAccess.delete(nodeHomePath.resolve(npx + cmd));

fileAccess.copy(npmBinBath.resolve(npm), nodeHomePath.resolve(npm));
fileAccess.copy(npmBinBath.resolve(npm + cmd), nodeHomePath.resolve(npm + cmd));
fileAccess.copy(npmBinBath.resolve(npx), nodeHomePath.resolve(npx));
fileAccess.copy(npmBinBath.resolve(npx + cmd), nodeHomePath.resolve(npx + cmd));
if(context.getSystemInfo().isWindows()) {
Path npmBinBath = nodeHomePath.resolve("node_modules/npm/bin/");
String npm = "npm";
String npx = "npx";
String cmd = ".cmd";

fileAccess.delete(nodeHomePath.resolve(npm));
fileAccess.delete(nodeHomePath.resolve(npm + cmd));
fileAccess.delete(nodeHomePath.resolve(npx));
fileAccess.delete(nodeHomePath.resolve(npx + cmd));

fileAccess.copy(npmBinBath.resolve(npm), nodeHomePath.resolve(npm));
fileAccess.copy(npmBinBath.resolve(npm + cmd), nodeHomePath.resolve(npm + cmd));
fileAccess.copy(npmBinBath.resolve(npx), nodeHomePath.resolve(npx));
fileAccess.copy(npmBinBath.resolve(npx + cmd), nodeHomePath.resolve(npx + cmd));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.devonfw.tools.ide.commandlet;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -28,14 +26,14 @@ public class InstallCommandletTest extends AbstractIdeContextTest {
private static Path resourcePath = Path.of("src/test/resources");

@BeforeAll
static void setUp() throws IOException {
static void setUp() {

server = new WireMockServer(WireMockConfiguration.wireMockConfig().port(1111));
server.start();
}

@AfterAll
static void tearDown() throws IOException {
static void tearDown() {

server.shutdownServer();
}
Expand Down
8 changes: 3 additions & 5 deletions cli/src/test/java/com/devonfw/tools/ide/tool/jmc/JmcTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.devonfw.tools.ide.tool.jmc;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -34,7 +32,7 @@ public class JmcTest extends AbstractIdeContextTest {
private static final Path RESOURCE_PATH = Path.of("src/test/resources");

@BeforeAll
static void setUp() throws IOException {
static void setUp() {

// TODO use random port number and create url file dynamically in project
// TODO ISSUE:https://github.com/devonfw/IDEasy/issues/223
Expand All @@ -44,7 +42,7 @@ static void setUp() throws IOException {
}

@AfterAll
static void tearDown() throws IOException {
static void tearDown() {

server.shutdownServer();
}
Expand Down
57 changes: 57 additions & 0 deletions cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,64 @@
package com.devonfw.tools.ide.tool.npm;

import java.io.IOException;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.commandlet.CommandLetExtractorMock;
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.repo.ToolRepositoryMock;

public class NpmTest extends AbstractIdeContextTest {

@Test
public void npmPostInstallShouldMoveFiles() throws IOException {
// arrange
String path = "workspaces/foo-test/my-git-repo";
String projectTestCaseName = "npm";

ToolRepositoryMock toolRepositoryMock = buildToolRepositoryMockForNpm(projectTestCaseName);

IdeContext context = newContext(projectTestCaseName, path, true, toolRepositoryMock);
toolRepositoryMock.setContext(context);

CommandLetExtractorMock commandLetExtractorMock = new CommandLetExtractorMock(context);
Npm commandlet = new Npm(context);
commandlet.setCommandletFileExtractor(commandLetExtractorMock);

assertThat(context.getSoftwarePath().resolve("node/npm")).hasContent("# This is npm");
assertThat(context.getSoftwarePath().resolve("node/npx")).hasContent("# This is npx");

// act
commandlet.install();

// assert
String expectedMessage = "Successfully installed npm in version 9.9.2";
assertLogMessage((IdeTestContext) context, IdeLogLevel.SUCCESS, expectedMessage, false);
if (context.getSystemInfo().isWindows()) {
Path test = context.getSoftwarePath();
assertThat(context.getSoftwarePath().resolve("node/npm")).exists();
assertThat(context.getSoftwarePath().resolve("node/npm.cmd")).exists();
assertThat(context.getSoftwarePath().resolve("node/npx")).exists();
assertThat(context.getSoftwarePath().resolve("node/npx.cmd")).exists();

assertThat(context.getSoftwarePath().resolve("node/npm")).hasContent("# This is npm bin");
assertThat(context.getSoftwarePath().resolve("node/npx")).hasContent("# This is npx bin");
}
}

private static ToolRepositoryMock buildToolRepositoryMockForNpm(String projectTestCaseName) {

String windowsFileFolder = "npm-9.9.2";

ToolRepositoryMock toolRepositoryMock = new ToolRepositoryMock("npm", "9.9.2", projectTestCaseName,
windowsFileFolder, "", "");

toolRepositoryMock.addAlreadyInstalledTool("node", "v18.19.1");

return toolRepositoryMock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.19.1

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

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

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

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 @@
# This is npm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Dummy npm 9.9.2 on windows> npmTestResult.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is npx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Dummy npx on windows> npxTestResult.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the tool repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dummy text file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# node-gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Dummy node-gyp> nodeGypTestResult.txt

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,16 @@
#********************************************************************************
# This file contains project specific environment variables defined by the user
#********************************************************************************

M2_REPO=~/.m2/repository

SOME=some-${UNDEFINED}

TEST_ARGS1=${TEST_ARGS1} conf1
TEST_ARGS2=${TEST_ARGS2} conf2
TEST_ARGS5=${TEST_ARGS5} conf5
TEST_ARGS6=${TEST_ARGS6} conf6
TEST_ARGS7=${TEST_ARGS7} conf7
TEST_ARGS8=${TEST_ARGS8} conf8
TEST_ARGSa=${TEST_ARGS1} ${TEST_ARGS3} confa
TEST_ARGSc=${TEST_ARGSc} confc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#********************************************************************************
# This file contains the global configuration from the user HOME directory.
#********************************************************************************

DOCKER_EDITION=docker
FOO=foo-${BAR}

TEST_ARGS1=${TEST_ARGS1} user1
TEST_ARGS2=${TEST_ARGS2} user2
TEST_ARGS3=${TEST_ARGS3} user3
TEST_ARGS7=user7
TEST_ARGS10=user10
TEST_ARGSb=userb
TEST_ARGSc=${TEST_ARGS1} userc
TEST_ARGSd=${TEST_ARGS1} userd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin_url=https://raw.githubusercontent.com/iloveeclipse/plugins/latest/
plugin_id=AnyEditTools.feature.group
plugin_active=false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the download cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the users HOME directory
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/npm/project/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_HOME directory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
the IDE CLI bash script
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin_url=https://raw.githubusercontent.com/iloveeclipse/plugins/latest/
plugin_id=AnyEditTools.feature.group
plugin_active=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin_url=https://checkstyle.org/eclipse-cs-update-site
plugin_id=net.sf.eclipsecs.feature.group
plugin_active=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#********************************************************************************
# This file contains project specific environment variables
#********************************************************************************

JAVA_VERSION=17*
MVN_VERSION=3.9.*
ECLIPSE_VERSION=2023-03
INTELLIJ_EDITION=ultimate

IDE_TOOLS=mvn,eclipse

BAR=bar-${SOME}

TEST_ARGS1=${TEST_ARGS1} settings1
TEST_ARGS4=${TEST_ARGS4} settings4
TEST_ARGS5=${TEST_ARGS5} settings5
TEST_ARGS6=${TEST_ARGS6} settings6
TEST_ARGS7=${TEST_ARGS7} settings7
TEST_ARGS8=settings8
TEST_ARGS9=settings9
TEST_ARGSb=${TEST_ARGS10} settingsb ${TEST_ARGSa} ${TEST_ARGSb}
TEST_ARGSc=${TEST_ARGSc} settingsc
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/npm/project/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
just a marker for detection of IDE_HOME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.19.1

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

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

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

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

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

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

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

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

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

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

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

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

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 @@
# This is npm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Dummy npm 9.9.2 on windows> npmTestResult.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is npx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Dummy npx on windows> npxTestResult.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#********************************************************************************
# Type of {@link EnvironmentVariables} from the
# {@link com.devonfw.tools.ide.context.IdeContext#getWorkspacePath() workspace directory}.
#********************************************************************************
TEST_ARGS1=${TEST_ARGS1} workspace1
TEST_ARGS3=${TEST_ARGS3} workspace3
TEST_ARGS6=${TEST_ARGS6} workspace6
TEST_ARGS7=${TEST_ARGS7} workspace7
TEST_ARGS8=${TEST_ARGS8} workspace8
TEST_ARGS9=${TEST_ARGS9} workspace9
TEST_ARGS10=${TEST_ARGS10} workspace10
TEST_ARGSd=${TEST_ARGSd} workspaced
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
my-git-repo in foo-test workspace of jmc test case
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the foo-test workspace of jmc test case
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the main workspace of jmc test case
1 change: 1 addition & 0 deletions cli/src/test/resources/ide-projects/npm/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is the IDE_ROOT directory

0 comments on commit f646b4d

Please sign in to comment.