Skip to content

Commit

Permalink
Attempt at renaming for clarity
Browse files Browse the repository at this point in the history
We also get rid of return values when not reachable
We also move the `-otp-` part of the Elixir version upstream so it's easy to reason about
  (and update the tests accordingly)
  • Loading branch information
paulo-ferraz-oliveira committed Mar 29, 2021
1 parent 0d4eb28 commit dfc4083
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 127 deletions.
16 changes: 7 additions & 9 deletions __tests__/setup-beam.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ async function testFailInstallElixir() {
let exVersion
let exOTPMajor

exVersion = '0.11'
exOTPMajor = '23'
exVersion = '0.11-otp-23'
await assert.rejects(
async () => {
await installer.installElixir(exVersion, exOTPMajor)
await installer.installElixir(exVersion)
},
(err) => {
assert.ok(err instanceof Error)
Expand All @@ -49,11 +48,10 @@ async function testFailInstallElixir() {
`Installing Elixir ${exVersion} with Erlang/OTP ${exOTPMajor} is supposed to fail`,
)

exVersion = '1.10.3'
exOTPMajor = '20'
exVersion = '1.10.3-otp-20'
await assert.rejects(
async () => {
await installer.installElixir(exVersion, exOTPMajor)
await installer.installElixir(exVersion)
},
(err) => {
assert.ok(err instanceof Error)
Expand Down Expand Up @@ -112,7 +110,7 @@ async function testElixirVersions() {

spec = '1.10.x'
otpVersion = 'OTP-23'
expected = ['v1.10.4', '23']
expected = 'v1.10.4-otp-23'
got = await setupElixir.getElixirVersion(
spec,
otpVersion,
Expand All @@ -122,7 +120,7 @@ async function testElixirVersions() {

spec = '^1.10'
otpVersion = 'OTP-23'
expected = ['v1.10.4', '23']
expected = 'v1.10.4-otp-23'
got = await setupElixir.getElixirVersion(
spec,
otpVersion,
Expand All @@ -132,7 +130,7 @@ async function testElixirVersions() {

spec = '1.11.0-rc.0'
otpVersion = 'OTP-23'
expected = ['v1.11.0-rc.0', '23']
expected = 'v1.11.0-rc.0-otp-23'
got = await setupElixir.getElixirVersion(
spec,
otpVersion,
Expand Down
117 changes: 60 additions & 57 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4562,56 +4562,56 @@ const path = __nccwpck_require__(5622)
* Install Erlang/OTP.
*
* @param {string} osVersion
* @param {string} version
* @param {string} otpVersion
*/
async function installOTP(osVersion, version) {
async function installOTP(osVersion, otpVersion) {
checkPlatform()
await exec(__nccwpck_require__.ab + "install-otp", [osVersion, version]).catch(
(err) => {
core.error(
`Installing Erlang/OTP ${otpVersion} over ${otpOSVersion} failed` +
'It is possible one of the requested versions does not exist',
)
throw new Error(err)
},
)
await exec(__nccwpck_require__.ab + "install-otp", [
osVersion,
otpVersion,
]).catch((err) => {
core.error(
`Installing Erlang/OTP ${otpVersion} over ${otpOSVersion} failed` +
'It is possible the requested version does not exist',
)
throw new Error(err)
})
}

/**
* Install Elixir.
*
* @param {string} version
* @param {string} otpMajor
* @param {string} elixirVersion
*/
async function installElixir(version, otpMajor) {
async function installElixir(elixirVersion) {
checkPlatform()
const otpString = otpMajor ? `-otp-${otpMajor}` : ''
await exec(__nccwpck_require__.ab + "install-elixir", [
version,
otpString,
]).catch((err) => {
core.error(
`Installing Elixir ${exVersion} with Erlang/OTP ${exOTPMajor} failed` +
'It is possible one of the requested versions does not exist',
)
throw new Error(err)
})
await exec(__nccwpck_require__.ab + "install-elixir", [elixirVersion]).catch(
(err) => {
core.error(
`Installing Elixir ${exVersion} failed` +
'It is possible the requested version does not exist',
)
throw new Error(err)
},
)
}

/**
* Install rebar3.
*
* @param {string} version
* @param {string} rebar3Version
*/
async function installRebar3(version) {
async function installRebar3(rebar3Version) {
checkPlatform()
await exec(__nccwpck_require__.ab + "install-rebar3", [version]).catch((err) => {
core.error(
`Installing rebar3 ${r3Version} failed` +
'It is possible the requested versions does not exist',
)
throw new Error(err)
})
await exec(__nccwpck_require__.ab + "install-rebar3", [rebar3Version]).catch(
(err) => {
core.error(
`Installing rebar3 ${r3Version} failed` +
'It is possible the requested version does not exist',
)
throw new Error(err)
},
)
}

function checkPlatform() {
Expand Down Expand Up @@ -4693,13 +4693,13 @@ function maybeInstallElixir(otpVersion, elixirBuildsListing) {
return new Promise(async (resolve) => {
const elixirSpec = core.getInput('elixir-version', {required: false})
if (elixirSpec) {
const [elixirVersion, otpMajor] = await getElixirVersion(
const elixirVersion = await getElixirVersion(
elixirSpec,
otpVersion,
elixirBuildsListing,
)
console.log(`##[group]Installing Elixir ${elixirVersion}`)
await installer.installElixir(elixirVersion, otpMajor)
await installer.installElixir(elixirVersion)
console.log(`##[endgroup]`)
core.setOutput('elixir-version', elixirVersion)
const matchersPath = __nccwpck_require__.ab + ".github"
Expand Down Expand Up @@ -4753,66 +4753,69 @@ function maybeInstallRebar3(rebar3BuildsListing) {
})
}

async function getOTPVersion(spec, osVersion, otpBuildsListing) {
async function getOTPVersion(otpSpec, osVersion, otpBuildsListing) {
const version = getVersionFromSpec(
spec,
otpSpec,
await getOTPVersions(osVersion, otpBuildsListing),
)
if (version == null) {
throw new Error(
`Requested Erlang/OTP version (from spec ${spec}) not found in build listing ` +
`Requested Erlang/OTP version (from spec ${otpSpec}) not found in build listing ` +
JSON.stringify(otpBuildsListing),
)
}
return version ? `OTP-${version}` : spec
const otpVersion = `OTP-${version}`
return otpVersion
}

async function getElixirVersion(spec, otpVersion, elixirBuildsListing) {
async function getElixirVersion(exSpec, otpVersion, elixirBuildsListing) {
const versions = await getElixirVersions(elixirBuildsListing)
const semverRegex = /^v(\d+\.\d+\.\d+(?:-.+)?)/

const semverVersions = Array.from(versions.keys())
.filter((str) => str.match(semverRegex))
.map((str) => str.match(semverRegex)[1])

const version = getVersionFromSpec(spec, semverVersions)
const version = getVersionFromSpec(exSpec, semverVersions)
if (version == null) {
throw new Error(
`Requested Elixir version (from spec ${spec}) not found in build listing ` +
`Requested Elixir version (from spec ${exSpec}) not found in build listing ` +
JSON.stringify(elixirBuildsListing),
)
}
const gitRef = version ? `v${version}` : spec
const exVersion = `v${version}`
const otpMatch = otpVersion.match(/^OTP-([^\.]*)/)

if (
otpMatch &&
versions.get(gitRef) &&
versions.get(gitRef).includes(otpMatch[1])
!(
otpMatch &&
versions.get(exVersion) &&
versions.get(exVersion).includes(otpMatch[1])
)
) {
return [gitRef, otpMatch[1]]
} else {
throw new Error(
`Requested Elixir / Erlang/OTP version (from spec ${spec} / ${otpVersion}) not found in ` +
`Requested Elixir / Erlang/OTP version (from spec ${exSpec} / ${otpVersion}) not found in ` +
`build listing ` +
JSON.stringify(elixirBuildsListing),
)
return [gitRef, null]
}

const elixirVersion = exVersion + `-otp-${otpMatch[1]}`
return elixirVersion
}

async function getRebar3Version(spec, rebar3BuildsListing) {
const version = getVersionFromSpec(
spec,
async function getRebar3Version(r3Spec, rebar3BuildsListing) {
const rebar3Version = getVersionFromSpec(
r3Spec,
await getRebar3Versions(rebar3BuildsListing),
)
if (version == null) {
if (rebar3Version == null) {
throw new Error(
`Requested rebar3 version (from spec ${spec}) not found in build listing ` +
`Requested rebar3 version (from spec ${r3Spec}) not found in build listing ` +
JSON.stringify(rebar3BuildsListing),
)
}
return version
return rebar3Version
}

async function getOTPVersions(osVersion, otpBuildsListing) {
Expand Down
3 changes: 2 additions & 1 deletion dist/install-elixir
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ set -eo pipefail

cd "$RUNNER_TEMP"

FILE_INPUT=${1}${2}.zip
VSN=${1}
FILE_INPUT="${VSN}.zip"
FILE_OUTPUT=elixir.zip
DIR_FOR_BIN=.setup-elixir/elixir

Expand Down
3 changes: 2 additions & 1 deletion dist/install-otp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ set -eo pipefail
cd "$RUNNER_TEMP"

OS=${1}
FILE_INPUT=${2}.tar.gz
VSN=${2}
FILE_INPUT="${VSN}.tar.gz"
FILE_OUTPUT=otp.tar.gz
DIR_FOR_BIN=.setup-elixir/otp

Expand Down
3 changes: 2 additions & 1 deletion src/install-elixir
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ set -eo pipefail

cd "$RUNNER_TEMP"

FILE_INPUT=${1}${2}.zip
VSN=${1}
FILE_INPUT="${VSN}.zip"
FILE_OUTPUT=elixir.zip
DIR_FOR_BIN=.setup-elixir/elixir

Expand Down
3 changes: 2 additions & 1 deletion src/install-otp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ set -eo pipefail
cd "$RUNNER_TEMP"

OS=${1}
FILE_INPUT=${2}.tar.gz
VSN=${2}
FILE_INPUT="${VSN}.tar.gz"
FILE_OUTPUT=otp.tar.gz
DIR_FOR_BIN=.setup-elixir/otp

Expand Down
68 changes: 34 additions & 34 deletions src/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,56 @@ const path = require('path')
* Install Erlang/OTP.
*
* @param {string} osVersion
* @param {string} version
* @param {string} otpVersion
*/
async function installOTP(osVersion, version) {
async function installOTP(osVersion, otpVersion) {
checkPlatform()
await exec(path.join(__dirname, 'install-otp'), [osVersion, version]).catch(
(err) => {
core.error(
`Installing Erlang/OTP ${otpVersion} over ${otpOSVersion} failed` +
'It is possible one of the requested versions does not exist',
)
throw new Error(err)
},
)
await exec(path.join(__dirname, 'install-otp'), [
osVersion,
otpVersion,
]).catch((err) => {
core.error(
`Installing Erlang/OTP ${otpVersion} over ${otpOSVersion} failed` +
'It is possible the requested version does not exist',
)
throw new Error(err)
})
}

/**
* Install Elixir.
*
* @param {string} version
* @param {string} otpMajor
* @param {string} elixirVersion
*/
async function installElixir(version, otpMajor) {
async function installElixir(elixirVersion) {
checkPlatform()
const otpString = otpMajor ? `-otp-${otpMajor}` : ''
await exec(path.join(__dirname, 'install-elixir'), [
version,
otpString,
]).catch((err) => {
core.error(
`Installing Elixir ${exVersion} with Erlang/OTP ${exOTPMajor} failed` +
'It is possible one of the requested versions does not exist',
)
throw new Error(err)
})
await exec(path.join(__dirname, 'install-elixir'), [elixirVersion]).catch(
(err) => {
core.error(
`Installing Elixir ${exVersion} failed` +
'It is possible the requested version does not exist',
)
throw new Error(err)
},
)
}

/**
* Install rebar3.
*
* @param {string} version
* @param {string} rebar3Version
*/
async function installRebar3(version) {
async function installRebar3(rebar3Version) {
checkPlatform()
await exec(path.join(__dirname, 'install-rebar3'), [version]).catch((err) => {
core.error(
`Installing rebar3 ${r3Version} failed` +
'It is possible the requested versions does not exist',
)
throw new Error(err)
})
await exec(path.join(__dirname, 'install-rebar3'), [rebar3Version]).catch(
(err) => {
core.error(
`Installing rebar3 ${r3Version} failed` +
'It is possible the requested version does not exist',
)
throw new Error(err)
},
)
}

function checkPlatform() {
Expand Down
Loading

0 comments on commit dfc4083

Please sign in to comment.