Skip to content

Commit

Permalink
Install dev deps in mta build (#1685)
Browse files Browse the repository at this point in the history
Ensure npm dev dependencies are available after mtaBuild as they are required by certain tests.
Co-authored-by: Kevin Hudemann <[email protected]>
Co-authored-by: Daniel Kurzynski <[email protected]>
  • Loading branch information
fwilhe authored Jun 22, 2020
1 parent 60fa1d5 commit 60eefab
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 3 deletions.
14 changes: 12 additions & 2 deletions cmd/mtaBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,18 @@ func runMtaBuild(config mtaBuildOptions,

commonPipelineEnvironment.mtarFilePath = mtarName

err = installMavenArtifacts(e, config)

if config.InstallArtifacts {
// install maven artifacts in local maven repo because `mbt build` executes `mvn package -B`
err = installMavenArtifacts(e, config)
if err != nil {
return err
}
// mta-builder executes 'npm install --production', therefore we need 'npm ci/install' to install the dev-dependencies
err = npmExecutor.InstallAllDependencies(npmExecutor.FindPackageJSONFiles())
if err != nil {
return err
}
}
return err
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/mtaBuild_generated.go

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

69 changes: 68 additions & 1 deletion integration/integration_mta_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ cd /test
apt-get -yqq update; apt-get -yqq install make
curl -OL https://github.com/SAP/cloud-mta-build-tool/releases/download/v1.0.14/cloud-mta-build-tool_1.0.14_Linux_amd64.tar.gz
tar xzf cloud-mta-build-tool_1.0.14_Linux_amd64.tar.gz
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -yqq nodejs
mv mbt /usr/bin
mkdir mym2
/piperbin/piper mtaBuild --m2Path=mym2 >test-log.txt 2>&1
/piperbin/piper mtaBuild --installArtifacts --m2Path=mym2 >test-log.txt 2>&1
`
ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)

Expand Down Expand Up @@ -79,6 +81,7 @@ mkdir mym2
assert.Contains(t, output, "Installing /test/.flattened-pom.xml to /test/mym2/mygroup/mymvn/1.0-SNAPSHOT/mymvn-1.0-SNAPSHOT.pom")
assert.Contains(t, output, "Installing /test/app/target/mymvn-app-1.0-SNAPSHOT.war to /test/mym2/mygroup/mymvn-app/1.0-SNAPSHOT/mymvn-app-1.0-SNAPSHOT.war")
assert.Contains(t, output, "Installing /test/app/target/mymvn-app-1.0-SNAPSHOT-classes.jar to /test/mym2/mygroup/mymvn-app/1.0-SNAPSHOT/mymvn-app-1.0-SNAPSHOT-classes.jar")
assert.Contains(t, output, "added 2 packages from 3 contributors and audited 2 packages in")
}

func TestNPMProject(t *testing.T) {
Expand Down Expand Up @@ -144,3 +147,67 @@ mv mbt /usr/bin
output := string(content)
assert.Contains(t, output, "INFO the MTA archive generated at: test-mta-js.mtar")
}

func TestNPMProjectInstallsDevDependencies(t *testing.T) {
t.Parallel()
ctx := context.Background()

pwd, err := os.Getwd()
if err != nil {
t.Fatalf("Getting current working directory failed: %v", err)
}
pwd = filepath.Dir(pwd)

// using custom createTmpDir function to avoid issues with symlinks on Docker for Mac
tempDir, err := createTmpDir("")
defer os.RemoveAll(tempDir) // clean up

if err != nil {
t.Fatalf("Error when creating temp dir: %v", err)
}

err = copyDir(filepath.Join(pwd, "integration", "testdata", "TestMtaIntegration", "npm-install-dev-dependencies"), tempDir)
if err != nil {
t.Fatal("Failed to copy test project.")
}

//workaround to use test script util it is possible to set workdir for Exec call
testScript := `#!/bin/sh
cd /test
apt-get -yqq update; apt-get -yqq install make
curl -OL https://github.com/SAP/cloud-mta-build-tool/releases/download/v1.0.14/cloud-mta-build-tool_1.0.14_Linux_amd64.tar.gz
tar xzf cloud-mta-build-tool_1.0.14_Linux_amd64.tar.gz
mv mbt /usr/bin
/piperbin/piper mtaBuild --installArtifacts >test-log.txt 2>&1
`
ioutil.WriteFile(filepath.Join(tempDir, "runPiper.sh"), []byte(testScript), 0700)

reqNode := testcontainers.ContainerRequest{
Image: "node:12",
Cmd: []string{"tail", "-f"},

BindMounts: map[string]string{
pwd: "/piperbin",
tempDir: "/test",
},
}

mbtContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: reqNode,
Started: true,
})

code, err := mbtContainer.Exec(ctx, []string{"sh", "/test/runPiper.sh"})

if err != nil {
t.Fatalf("Script returened error: %v", err)
}
assert.Equal(t, 0, code)

content, err := ioutil.ReadFile(filepath.Join(tempDir, "/test-log.txt"))
if err != nil {
t.Fatal("Could not read test-log.txt.", err)
}
output := string(content)
assert.Contains(t, output, "added 2 packages in")
}
2 changes: 2 additions & 0 deletions integration/testdata/TestMtaIntegration/maven/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ foo.mtar
.flattened-pom.xml
.pipeline/
mym2
app/node_modules
app/package-lock.json
10 changes: 10 additions & 0 deletions integration/testdata/TestMtaIntegration/maven/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "my-awesome-package",
"version": "1.0.0",
"dependencies": {
"uuid": "^8.1.0"
},
"devDependencies": {
"is-number": "^7.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.pipeline
node_modules
package-lock.json
test-mta-js.mtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# shellcheck disable=SC2002
cat .gitignore | xargs -L1 rm -r
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_schema-version: '3.1'
ID: test-mta-js
version: 1.0.0

modules:
- name: test-mta-js-srv
type: nodejs
path: .
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "my-awesome-package",
"version": "1.0.0",
"dependencies": {
"uuid": "^8.1.0"
},
"devDependencies": {
"is-number": "^7.0.0"
}
}
8 changes: 8 additions & 0 deletions resources/metadata/mtaBuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ spec:
mandatory: false
aliases:
- name: maven/m2Path
- name: installArtifacts
type: bool
description: If enabled, for npm packages this step will install all depedencies including dev dependencies. For maven it will install all artifacts to the local maven repository.
scope:
- GENERAL
- STEPS
- STAGES
- PARAMETERS
outputs:
resources:
- name: commonPipelineEnvironment
Expand Down

0 comments on commit 60eefab

Please sign in to comment.