Skip to content

Commit

Permalink
Confirm that fast npm install works even if you delete node_modules #310
Browse files Browse the repository at this point in the history
  • Loading branch information
deepy committed May 4, 2024
1 parent 92e1d21 commit 1e85346
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ abstract class NpmInstallTask : NpmTask() {
return null
}

// When legacy npm support is dropped this does not need projectFileIfExists
return projectFileIfExists("node_modules/.package-lock.json").orNull
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.gradle.node.npm.task

import com.github.gradle.AbstractIntegTest
import com.github.gradle.node.NodeExtension
import groovy.io.FileType
import org.gradle.testkit.runner.TaskOutcome
import org.gradle.util.GradleVersion
import spock.lang.IgnoreIf
Expand Down Expand Up @@ -94,6 +95,133 @@ class NpmInstall_integTest extends AbstractIntegTest {
gv << GRADLE_VERSIONS_UNDER_TEST
}
def 'fast npm install properly handles lockfile deleted through task (#gv.version)'() {
given:
gradleVersion = gv
writePackageJson '''
{
"name": "fastinstall",
"dependencies": {
"@isaacs/string-locale-compare": "1.1.0"
}
}
'''
def scriptfile = "script.js"
writeFile(scriptfile, """
const stringLocaleCompare = require('@isaacs/string-locale-compare')
console.log(['b', 'a'].sort(stringLocaleCompare('en')))
""")
writeBuild("""
plugins {
id 'com.github.node-gradle.node'
}
node {
fastNpmInstall = true
}
def scriptWithDependency = file("$scriptfile")
tasks.register("taskWithDependency", NodeTask) {
script = scriptWithDependency
dependsOn "npmInstall"
}
tasks.register("deleteNodeModules", Delete) {
delete "node_modules"
}
""")
when:
def result = build('npmInstall')
then:
result.task(":npmInstall").outcome == TaskOutcome.SUCCESS
when:
// when the node_modules is removed
result = build('deleteNodeModules')
then:
result.task(':deleteNodeModules').outcome == TaskOutcome.SUCCESS
when:
result = build('taskWithDependency')
then:
// npmInstall is re-run and the task runs successfully
result.task(":npmInstall").outcome == TaskOutcome.SUCCESS
result.task(":taskWithDependency").outcome == TaskOutcome.SUCCESS
result.output.contains("[ 'a', 'b' ]")
where:
gv << GRADLE_VERSIONS_UNDER_TEST
}
def 'fast npm install properly handles lockfile deleted manually (#gv.version)'() {
given:
gradleVersion = gv
writePackageJson '''
{
"name": "fastinstall",
"dependencies": {
"@isaacs/string-locale-compare": "1.1.0"
}
}
'''
def scriptfile = "script.js"
writeFile(scriptfile, """
const stringLocaleCompare = require('@isaacs/string-locale-compare')
console.log(['b', 'a'].sort(stringLocaleCompare('en')))
""")
writeBuild("""
plugins {
id 'com.github.node-gradle.node'
}
node {
fastNpmInstall = true
}
def scriptWithDependency = file("$scriptfile")
tasks.register("taskWithDependency", NodeTask) {
script = scriptWithDependency
dependsOn "npmInstall"
}
""")
when:
def result = build('npmInstall')
then:
result.task(":npmInstall").outcome == TaskOutcome.SUCCESS
when:
// when the node_modules is removed
projectDir.eachFile(FileType.DIRECTORIES, {
if (it.name == "node_modules") {
it.deleteDir()
}
})
result = build('taskWithDependency')
then:
// npmInstall is re-run and the task runs successfully
result.task(":npmInstall").outcome == TaskOutcome.SUCCESS
result.task(":taskWithDependency").outcome == TaskOutcome.SUCCESS
result.output.contains("[ 'a', 'b' ]")
where:
gv << GRADLE_VERSIONS_UNDER_TEST
}
def 'install packages with npm >= 7 (#gv.version)'() {
given:
gradleVersion = gv
Expand Down

0 comments on commit 1e85346

Please sign in to comment.