Skip to content

Commit

Permalink
Move unsupported platform failure from os.name to separate method.
Browse files Browse the repository at this point in the history
Previously we've accidentally depended on the isWindows check
to see if we're on a supported platform or not, this had
the unfortunate side-effect that even with `download = false`
we triggered the error(). See #178
  • Loading branch information
deepy committed Jul 4, 2021
1 parent 4d083a7 commit febf536
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ abstract class NodeSetupTask : DefaultTask() {

@TaskAction
fun exec() {
failIfUnsupportedPlatform()
deleteExistingNode()
unpackNodeArchive()
setExecutableFlag()
}

private fun failIfUnsupportedPlatform() {
PlatformHelper.INSTANCE.failOnUnsupportedOs()
}

private fun deleteExistingNode() {
projectHelper.delete {
delete(nodeDir.get().dir("../"))
Expand Down
16 changes: 14 additions & 2 deletions src/main/kotlin/com/github/gradle/node/util/PlatformHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ open class PlatformHelper constructor(private val props: Properties = System.get
name.contains("windows") -> "win"
name.contains("mac") -> "darwin"
name.contains("linux") -> "linux"
name.contains("freebsd") -> "linux"
name.contains("sunos") -> "sunos"
else -> error("Unsupported OS: $name")
else -> "unsupported"
}
}

Expand All @@ -32,6 +31,14 @@ open class PlatformHelper constructor(private val props: Properties = System.get

open val isWindows: Boolean by lazy { osName == "win" }

open val isSupported: Boolean by lazy { osName != "unsupported" }

fun failOnUnsupportedOs() {
if (!isSupported) {
error("Unsupported OS")
}
}

private fun property(name: String): String {
val value = props.getProperty(name)
return value ?: System.getProperty(name) ?:
Expand All @@ -48,6 +55,11 @@ open class PlatformHelper constructor(private val props: Properties = System.get
fun main(args: Array<String>) {
println("Your os.name is: '${System.getProperty("os.name")}' and is parsed as: ${PlatformHelper.INSTANCE.osName}")
println("Your os.arch is: '${System.getProperty("os.arch")}' and is parsed as: ${PlatformHelper.INSTANCE.osArch}")
if (!PlatformHelper.INSTANCE.isSupported) {
println("Your platform is \"unsupported\" (isSupported == false)")
println("Your platform does not support 'download = true' as there's no official Node.js binaries" +
" being published for it. You can still use the plugin, but you need to install Node.js manually")
}
if (PlatformHelper.INSTANCE.isWindows) {
println("You're on windows (isWindows == true)")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ class PlatformHelperTest extends Specification {
this.helper.getOsName() == osName
this.helper.getOsArch() == osArch
this.helper.isWindows() == isWindows
this.helper.isSupported() == isSupported

where:
osProp | archProp | osName | osArch | isWindows
'Windows 8' | 'x86' | 'win' | 'x86' | true
'Windows 8' | 'x86_64' | 'win' | 'x64' | true
'Mac OS X' | 'x86' | 'darwin' | 'x86' | false
'Mac OS X' | 'x86_64' | 'darwin' | 'x64' | false
'Linux' | 'x86' | 'linux' | 'x86' | false
'Linux' | 'x86_64' | 'linux' | 'x64' | false
'Linux' | 'ppc64le' | 'linux' | 'ppc64le' | false
'SunOS' | 'x86' | 'sunos' | 'x86' | false
'SunOS' | 'x86_64' | 'sunos' | 'x64' | false
osProp | archProp | osName | osArch | isWindows | isSupported
'Windows 8' | 'x86' | 'win' | 'x86' | true | true
'Windows 8' | 'x86_64' | 'win' | 'x64' | true | true
'Mac OS X' | 'x86' | 'darwin' | 'x86' | false | true
'Mac OS X' | 'x86_64' | 'darwin' | 'x64' | false | true
'Linux' | 'x86' | 'linux' | 'x86' | false | true
'Linux' | 'x86_64' | 'linux' | 'x64' | false | true
'Linux' | 'ppc64le' | 'linux' | 'ppc64le' | false | true
'SunOS' | 'x86' | 'sunos' | 'x86' | false | true
'SunOS' | 'x86_64' | 'sunos' | 'x64' | false | true
'FreeBSD' | 'amd64' | 'unsupported' | 'x64' | false | false
}

@Unroll
Expand Down Expand Up @@ -62,7 +64,7 @@ class PlatformHelperTest extends Specification {
this.props.setProperty("os.name", 'Nonsense')

when:
this.helper.getOsName()
this.helper.failOnUnsupportedOs()

then:
thrown(IllegalStateException)
Expand Down

0 comments on commit febf536

Please sign in to comment.