Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Npmrc generation only executed if nedded (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquimmnetto authored Sep 16, 2022
1 parent ab5b12c commit 6583977
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import spock.lang.Shared

class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {



@Shared
def version = "1.0.0"

Expand All @@ -39,10 +41,10 @@ class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables()


Grgit git

def setup() {

environmentVariables.set("GRGIT_USER", testUserName)
environmentVariables.set("GRGIT_PASS", testUserToken)
environmentVariables.set('NODE_RELEASE_NPM_USER', npmUser)
Expand Down Expand Up @@ -70,6 +72,7 @@ class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {
git.commit(message: 'initial commit')
git.tag.add(name: 'v0.0.1')
git.remote.add(name: "origin", url: "https://github.com/${testRepositoryName}.git")

}

def "run task of type NpmCredentialsTask with default properties"() {
Expand All @@ -79,8 +82,12 @@ class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {
task test (type:wooga.gradle.node.tasks.NpmCredentialsTask)
""".stripIndent()

expect: "runs"
runTasksSuccessfully("test")
when:
def result = runTasksSuccessfully("test")

then:
!result.wasSkipped(":test")
result.wasExecuted(":test")
}

def "run task of type NpmCredentialsTask with task properties"() {
Expand All @@ -94,13 +101,16 @@ class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {
}
""".stripIndent()

expect: "runs"
runTasksSuccessfully("test")
when:
def result = runTasksSuccessfully("test")

then:
!result.wasSkipped(":test")
result.wasExecuted(":test")
}

def "run task :ensureNpmrc with project properties"() {

when: "no env vars set for npm login"
given: "no env vars set for npm login"
environmentVariables.clear('NODE_RELEASE_NPM_USER', 'NODE_RELEASE_NPM_PASS')

and:
Expand All @@ -114,19 +124,23 @@ class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {
nodeRelease.npmAuthUrl=${npmAuthUrl}
""".stripIndent().trim()

then: "runs"
runTasksSuccessfully("ensureNpmrc")
when:
def result = runTasksSuccessfully("ensureNpmrc")

then:
!result.wasSkipped(":ensureNpmrc")
result.wasExecuted(":ensureNpmrc")
}

def "run task :ensureNpmrc with extension.properties"() {

when: "no env vars set for npm login"
given: "no env vars set for npm login"
environmentVariables.clear('NODE_RELEASE_NPM_USER', 'NODE_RELEASE_NPM_PASS')

and:
assert (!System.getenv("NODE_RELEASE_NPM_USER"))
assert (!System.getenv("NODE_RELEASE_NPM_PASS"))
assert runTasksWithFailure("ensureNpmrc")
assert runTasks("ensureNpmrc").wasSkipped("ensureNpmrc")

and: 'properties defined in properties file'
buildFile << """nodeRelease {
Expand All @@ -136,8 +150,31 @@ class NodeReleasePluginCredentialsSpec extends GithubIntegrationSpec {
}
""".stripIndent()

then: "runs"
runTasksSuccessfully("ensureNpmrc")
when:
def result = runTasksSuccessfully("ensureNpmrc")

then:
!result.wasSkipped(":ensureNpmrc")
result.wasExecuted(":ensureNpmrc")
}

def "skips task of type NpmCredentialsTask when no credentials set"() {

given: "a valid defined task"
buildFile << """
task test (type:wooga.gradle.node.tasks.NpmCredentialsTask) {
npmUser = null
npmPass = null
npmAuthUrl = null
}
""".stripIndent()

when:
def result = runTasksSuccessfully("test")

then:
result.wasSkipped(":test")
result.wasExecuted(":test")
}

def "task of type NpmCredentialsTask writes .npmrc file"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import spock.lang.Unroll

class NodeReleasePluginEngineSpec extends GithubIntegrationSpec {

static def homeNpmRc = new File(System.getProperty("user.home"), ".npmrc")
static def tmpHomeNpmRc = new File(System.getProperty("user.home"), ".npmrc.tmp")

@Shared
def version = "1.0.0"

Expand All @@ -45,6 +48,7 @@ class NodeReleasePluginEngineSpec extends GithubIntegrationSpec {
@Shared
File packageJsonFile


def setup() {

environmentVariables.set("GRGIT_USER", testUserName)
Expand All @@ -64,14 +68,14 @@ class NodeReleasePluginEngineSpec extends GithubIntegrationSpec {
group = 'test'
version = "$version"
${applyPlugin(NodeReleasePlugin)}
node.version = '10.5.0'
node.version = '18.7.0'
node.download = true
""".stripIndent()

packageJsonFile = createFile('package.json')
packageJsonFile.text = packageJsonContent([
"scripts" : ["clean": "shx echo \"clean\"", "test": "shx echo \"test\"", "build": "shx echo \"build\""],
"devDependencies": ["shx": "^0.3.2"],
"devDependencies": ["shx": "^0.3.4"],
])

git = Grgit.init(dir: projectDir)
Expand All @@ -88,9 +92,6 @@ class NodeReleasePluginEngineSpec extends GithubIntegrationSpec {
createFile(lockfile)
}

//and: "dependencies are installed"
//runTasksSuccessfully('npmSetup')

when:
"run task ${task}"
def result = runTasks(task)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class NodeReleasePluginPublishSpec extends GithubIntegrationSpec {
group = 'test'
version = "$version"
${applyPlugin(NodeReleasePlugin)}
node.version = '10.5.0'
node.version = '18.7.0'
node.download = true
github.repositoryName = '$testRepositoryName'
Expand Down
43 changes: 30 additions & 13 deletions src/main/groovy/wooga/gradle/node/NodeReleasePlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.gradle.api.plugins.BasePlugin
import org.gradle.api.plugins.ExtraPropertiesExtension
import org.gradle.api.publish.plugins.PublishingPlugin
import org.gradle.api.specs.Spec
import org.gradle.internal.impldep.org.apache.commons.lang.SystemUtils
import org.gradle.language.base.plugins.LifecycleBasePlugin
import wooga.gradle.github.publish.GithubPublishPlugin
import wooga.gradle.github.publish.tasks.GithubPublish
Expand Down Expand Up @@ -58,6 +59,7 @@ class NodeReleasePlugin implements Plugin<Project> {
static final String YARN_LOCK_JSON = 'yarn.lock'
static final String NPMRC = '.npmrc'

private Project project
private NodeReleasePluginExtension extension
private static Engine engine

Expand All @@ -68,7 +70,8 @@ class NodeReleasePlugin implements Plugin<Project> {
project.pluginManager.apply(NodePlugin.class)
project.pluginManager.apply(GithubPublishPlugin.class)

extension = createExtension(project)
this.project = project
this.extension = createExtension(project)

if (project == project.rootProject) {
detectEngine(project)
Expand Down Expand Up @@ -123,19 +126,24 @@ class NodeReleasePlugin implements Plugin<Project> {
})
}

private static void configureNpmCredentialsTasks(Project project, NodeReleasePluginExtension extension) {
project.tasks.withType(NpmCredentialsTask, new Action<NpmCredentialsTask>() {

@Override
void execute(NpmCredentialsTask npmCredentialsTask) {
npmCredentialsTask.group = TASK_GROUP
npmCredentialsTask.description = "create ${NPMRC} file"
npmCredentialsTask.npmUser.set(extension.npmUser)
npmCredentialsTask.npmPass.set(extension.npmPass)
npmCredentialsTask.npmAuthUrl.set(extension.npmAuthUrl)
npmCredentialsTask.npmrcFile.set(extension.npmrcFile)
private void configureNpmCredentialsTasks(Project project, NodeReleasePluginExtension extension) {
project.tasks.withType(NpmCredentialsTask).configureEach { task ->
task.group = TASK_GROUP
task.description = "create ${NPMRC} file"
task.npmUser.set(extension.npmUser)
task.npmPass.set(extension.npmPass)
task.npmAuthUrl.set(extension.npmAuthUrl)
task.npmrcFile.set(extension.npmrcFile)
task.onlyIf {
def hasCredentials = project.provider{ task.npmUser.present && task.npmPass.present}
.map(warnFalse{"username & password credentials not provided, skipping"})
.orElse(true)
def hasAuthUrl = project.provider{ task.npmAuthUrl.present}
.map(warnFalse{"npm authentication url not provided, skipping"})
.orElse(true)
return hasCredentials.get() && hasAuthUrl.get()
}
})
}
}

/**
Expand Down Expand Up @@ -261,4 +269,13 @@ class NodeReleasePlugin implements Plugin<Project> {
}
}
}

Closure<Boolean> warnFalse(Closure<String> message) {
return { it -> warnFalseCls(it, message())}
}

Closure<Boolean> warnFalseCls = { Boolean value, String message ->
if (!value) project.logger?.warn(message)
return value
}.memoize()
}

0 comments on commit 6583977

Please sign in to comment.