Skip to content

Commit

Permalink
test: improve the way to get environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
wdzeng committed Aug 16, 2024
1 parent 59464bc commit 37c0d44
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,27 @@ SwECHgMUAAAACACtpCJXncQPnp0AAAAZAQAADQAYAAAAAAABAAAApIE2AwAAbWFuaWZlc3QuanNv
blVUBQADhSzzZHV4CwABBOgDAAAE6AMAAFBLBQYAAAAABgAGAAICAAAaBAAAAAA=
`

function requireEnvironmentVariable(key: string): string {
const value = process.env[key]
if (!value) {
throw new Error(`Environment variable ${key} is required.`)
function getEnv(): {
addonGuid: string
jwtIssuer: string
jwtSecret: string
} {
const addonGuid = process.env.TEST_ADDON_GUID
const jwtIssuer = process.env.TEST_JWT_ISSUER
const jwtSecret = process.env.TEST_JWT_SECRET
if (!addonGuid || !jwtIssuer || !jwtSecret) {
if (process.env.GITHUB_ACTIONS) {
core.setFailed(
'Environment variables TEST_ADDON_GUID, TEST_JWT_ISSUER and TEST_JWT_SECRET are required. Did you set the secrets?'
)
process.exit(1)
}

throw new Error(
'Environment variables TEST_ADDON_GUID, TEST_JWT_ISSUER and TEST_JWT_SECRET are required. Did you have a .env.local file?'
)
}
return value
return { addonGuid, jwtIssuer, jwtSecret }
}

function updateVersionAndSaveZip(zipPath: string): void {
Expand Down Expand Up @@ -76,10 +91,7 @@ function updateVersionAndSaveZip(zipPath: string): void {
}

async function main() {
const addonGuid = requireEnvironmentVariable('TEST_ADDON_GUID')
const jwtIssuer = requireEnvironmentVariable('TEST_JWT_ISSUER')
const jwtSecret = requireEnvironmentVariable('TEST_JWT_SECRET')

const { addonGuid, jwtIssuer, jwtSecret } = getEnv()
const xpiPath = `${tmp.fileSync().name}.zip`
fs.writeFileSync(xpiPath, TEST_ADDON, 'base64')
updateVersionAndSaveZip(xpiPath)
Expand Down

0 comments on commit 37c0d44

Please sign in to comment.