Skip to content

Commit

Permalink
added unittests for npm
Browse files Browse the repository at this point in the history
  • Loading branch information
ndemirca committed Mar 1, 2024
1 parent f646b4d commit 800ae29
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void jmcPostInstallShouldMoveFilesIfRequired() throws IOException {
}

@Test
public void jmcShouldRunExecuteableSuccessful() throws IOException {
public void jmcShouldRunExecuteableSuccessful() {

// arrange
String path = "workspaces/foo-test/my-git-repo";
Expand Down
65 changes: 45 additions & 20 deletions cli/src/test/java/com/devonfw/tools/ide/tool/npm/NpmTest.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,80 @@
package com.devonfw.tools.ide.tool.npm;

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

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.commandlet.CommandLetExtractorMock;
import com.devonfw.tools.ide.context.AbstractIdeContext;
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);
private static AbstractIdeContext context;
private static Npm commandlet;
private final static String NPM_TEST_PATH = "workspaces/foo-test/my-git-repo";
private final static String PROJECT_TEST_CASE_NAME = "npm";
private static ToolRepositoryMock toolRepositoryMock;
private static Path mockResultPath;

@BeforeAll
static void setUp() {

toolRepositoryMock = buildToolRepositoryMockForNpm(PROJECT_TEST_CASE_NAME);

context = newContext(PROJECT_TEST_CASE_NAME, NPM_TEST_PATH, true, toolRepositoryMock);
toolRepositoryMock.setContext(context);
mockResultPath = Path.of("target/test-projects/npm/project");
context.setDefaultExecutionDirectory(mockResultPath);

CommandLetExtractorMock commandLetExtractorMock = new CommandLetExtractorMock(context);
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");
@Test
public void npmPostInstallShouldMoveFiles() {
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");
assertThat(context.getSoftwarePath().resolve("node/npm")).hasContent("This is npm bin");
assertThat(context.getSoftwarePath().resolve("node/npx")).hasContent("This is npx bin");
}
}

@Test
public void npmShouldRunExecutableSuccessful() {

//arrange
String expectedOutputWindowsNpm = "Dummy npm bin 9.9.2 on windows";

// act
commandlet.install();
commandlet.run();

//assert
assertThat(mockResultPath.resolve("npmTestResult.txt")).exists();
assertThat(mockResultPath.resolve("npmTestResult.txt")).hasContent(expectedOutputWindowsNpm);
}

private static ToolRepositoryMock buildToolRepositoryMockForNpm(String projectTestCaseName) {

String windowsFileFolder = "npm-9.9.2";
Expand Down

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
@@ -1 +1 @@
# This is npm
This is npm
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# This is npx
This is npx

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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
@@ -1 +1 @@
# This is npm
This is npm
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# This is npx
This is npx

0 comments on commit 800ae29

Please sign in to comment.