Skip to content

Commit c0a55c2

Browse files
committed
Refactor globals into a separate module
Signed-off-by: Andrei Horodniceanu <[email protected]>
1 parent c9c554c commit c0a55c2

File tree

7 files changed

+90
-53
lines changed

7 files changed

+90
-53
lines changed

__tests__/d-compiler.test.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import { Compiler } from '../src/d'
22
import fs from 'fs'
3+
import SETTINGS from '../src/settings'
34

45
describe('Test Compiler class', () => {
56
const logSpy = jest.spyOn(console, 'log').mockReturnValue(undefined)
67
jest.spyOn(process.stdout, 'write').mockReturnValue(true)
78

8-
let originalEnv = process.env
9-
afterEach(() => process.env = originalEnv)
9+
const originalEnv = process.env
10+
const originalSep = SETTINGS.sep
11+
const originalExeExt = SETTINGS.exeExt
12+
// This value is cached so it matches the host's
13+
const pathSep = (process.platform == 'win32' ? ';' : ':')
14+
15+
afterEach(() => {
16+
process.env = originalEnv
17+
SETTINGS.sep = originalSep
18+
SETTINGS.exeExt = originalExeExt
19+
})
1020

1121
beforeEach(() => logSpy.mockClear())
1222

@@ -17,13 +27,6 @@ describe('Test Compiler class', () => {
1727
let c = new Compiler('url', undefined, name, 'ver', bin, libs, dmdWrapper)
1828
const root = '/root/folder'
1929

20-
// The values are computed when d.ts is imported so
21-
// they will have the values of the host system, even
22-
// if process.platform is modified in the tests.
23-
const sep = process.platform == 'win32' ? '\\' : '/'
24-
const extension = process.platform == 'win32' ? '.exe' : ''
25-
const pathSep = (process.platform == 'win32' ? ';' : ':')
26-
2730
test('Test setting PATH', () => {
2831
process.env['PATH']='/bin'
2932
c.addBinPath(root)
@@ -88,6 +91,8 @@ describe('Test Compiler class', () => {
8891
test('Test makeAvailable', async () => {
8992
jest.spyOn(c, 'getCached').mockResolvedValue(root)
9093

94+
SETTINGS.sep = '/'
95+
SETTINGS.exeExt = ''
9196
for (const platform of [ 'linux', 'darwin', 'freebsd' ]) {
9297
Object.defineProperty(process, 'platform', { value: platform })
9398
jest.spyOn(fs, 'existsSync').mockReturnValue(true).
@@ -99,11 +104,13 @@ describe('Test Compiler class', () => {
99104

100105
expect(process.env['PATH']).toBe(root + bin + pathSep + '/bin')
101106
expect(process.env['LD_LIBRARY_PATH']).toBe(root + libs[1])
102-
expect(process.env['DC']).toBe(`${root}${bin}${sep}${name}${extension}`)
103-
expect(process.env['DMD']).toBe(`${root}${bin}${sep}${dmdWrapper}${extension}`)
107+
expect(process.env['DC']).toBe(`${root}${bin}/${name}`)
108+
expect(process.env['DMD']).toBe(`${root}${bin}/${dmdWrapper}`)
104109
}
105110

106111

112+
SETTINGS.sep = '\\'
113+
SETTINGS.exeExt = '.exe'
107114
Object.defineProperty(process, 'platform', { value: 'win32' })
108115
jest.spyOn(fs, 'existsSync').mockReturnValue(true)
109116

@@ -113,16 +120,18 @@ describe('Test Compiler class', () => {
113120
const expPath = `${root}${libs[1]}${pathSep}` + `${root}${libs[0]}${pathSep}` +
114121
`${root}${bin}${pathSep}` + '\\bin'
115122
expect(process.env['PATH']).toBe(expPath)
116-
expect(process.env['DC']).toBe(`${root}${bin}${sep}${name}${extension}`)
117-
expect(process.env['DMD']).toBe(`${root}${bin}${sep}${dmdWrapper}${extension}`)
123+
expect(process.env['DC']).toBe(`${root}${bin}\\${name}.exe`)
124+
expect(process.env['DMD']).toBe(`${root}${bin}\\${dmdWrapper}.exe`)
118125
})
119126

120127
test('DC and DMD get set to the absolute path of the compiler', () => {
121128
for (const platform of [ 'linux', 'win32', 'darwin', 'freebsd' ]) {
129+
const sep = SETTINGS.sep = '/'
130+
const exeExt = SETTINGS.exeExt = ''
122131
Object.defineProperty(process, 'platform', { value: platform })
123132
c.setDC(root)
124-
expect(process.env['DC']).toBe(root + bin + sep + name + extension)
125-
expect(process.env['DMD']).toBe(root + bin + sep + dmdWrapper + extension)
133+
expect(process.env['DC']).toBe(root + bin + sep + name + exeExt)
134+
expect(process.env['DMD']).toBe(root + bin + sep + dmdWrapper + exeExt)
126135
}
127136
})
128137
})

__tests__/d-dmd.test.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { DMD, SETTINGS } from '../src/d'
1+
import { DMD } from '../src/d'
22
import * as gpg from '../src/gpg'
33
import * as utils from '../src/utils'
44
import * as testUtils from './test-helpers.test'
55
import * as tc from '@actions/tool-cache'
66
import fs from 'fs'
77

8+
import SETTINGS from '../src/settings'
9+
810
testUtils.hideConsoleLogs()
911
testUtils.saveProcessRestorePoint()
1012
testUtils.disableNetwork()
@@ -442,10 +444,10 @@ test('dmd fails on unsupported platforms', async () => {
442444
describe('Test makeAvailable', () => {
443445
const root = '/tmp/cache'
444446
const origEnv = process.env
447+
const origSep = SETTINGS.sep
448+
const origExeExt = SETTINGS.exeExt
445449

446-
// These values are cached so they match the hosts
447-
const sep = (process.platform == 'win32' ? '\\' : '/')
448-
const exeExt = (process.platform == 'win32' ? '.exe' : '')
450+
// This value is cached so it matches the host's
449451
const pathSep = (process.platform == 'win32' ? ';' : ':')
450452

451453
beforeEach(() => {
@@ -455,30 +457,40 @@ describe('Test makeAvailable', () => {
455457
process.env['PATH'] = '/bin'
456458
process.env['LD_LIBRARY_PATH'] = ''
457459
})
458-
afterEach(() => process.env = origEnv)
460+
afterEach(() => {
461+
process.env = origEnv
462+
SETTINGS.sep = origSep
463+
SETTINGS.exeExt = origExeExt
464+
})
459465

460466
test('linux', async () => {
461467
Object.defineProperty(process, 'platform', { value: 'linux' })
468+
SETTINGS.sep = '/'
469+
SETTINGS.exeExt = ''
462470
const dmd = await init('dmd-2.109.1')
463471
await dmd.makeAvailable()
464472
expect(process.env['PATH']).toBe(root + '/dmd2/linux/bin64' + pathSep + '/bin')
465473
expect(process.env['LD_LIBRARY_PATH']).toBe(root + '/dmd2/linux/lib64')
466-
expect(process.env['DC']).toBe(root + `/dmd2/linux/bin64${sep}dmd${exeExt}`)
467-
expect(process.env['DMD']).toBe(root + `/dmd2/linux/bin64${sep}dmd${exeExt}`)
474+
expect(process.env['DC']).toBe(root + '/dmd2/linux/bin64/dmd')
475+
expect(process.env['DMD']).toBe(root + '/dmd2/linux/bin64/dmd')
468476
})
469477

470478
test('osx', async () => {
471479
Object.defineProperty(process, 'platform', { value: 'darwin' })
480+
SETTINGS.sep = '/'
481+
SETTINGS.exeExt = ''
472482
const dmd = await init('dmd-2.109.1')
473483
await dmd.makeAvailable()
474484
expect(process.env['PATH']).toBe(root + '/dmd2/osx/bin' + pathSep + '/bin')
475485
expect(process.env['LD_LIBRARY_PATH']).toBe(root + '/dmd2/osx/lib')
476-
expect(process.env['DC']).toBe(root + `/dmd2/osx/bin${sep}dmd${exeExt}`)
477-
expect(process.env['DMD']).toBe(root + `/dmd2/osx/bin${sep}dmd${exeExt}`)
486+
expect(process.env['DC']).toBe(root + '/dmd2/osx/bin/dmd')
487+
expect(process.env['DMD']).toBe(root + '/dmd2/osx/bin/dmd')
478488
})
479489

480490
test('windows', async () => {
481491
Object.defineProperty(process, 'platform', { value: 'win32' })
492+
SETTINGS.sep = '\\'
493+
SETTINGS.exeExt = '.exe'
482494
const dmd = await init('dmd-2.109.1')
483495
await dmd.makeAvailable()
484496

@@ -493,12 +505,12 @@ describe('Test makeAvailable', () => {
493505
// Check that the 64bit folder appears before the 32bit one
494506
expect(found64).toBeLessThan(found32)
495507

496-
expect(process.env['DC']).toBe(root + `\\dmd2\\windows\\bin64${sep}dmd${exeExt}`)
497-
expect(process.env['DMD']).toBe(root + `\\dmd2\\windows\\bin64${sep}dmd${exeExt}`)
508+
expect(process.env['DC']).toBe(root + `\\dmd2\\windows\\bin64\\dmd.exe`)
509+
expect(process.env['DMD']).toBe(root + `\\dmd2\\windows\\bin64\\dmd.exe`)
498510
})
499511

500512
describe('Check that verify_sig is respected', () => {
501-
const save = SETTINGS.verify_sig
513+
const save = SETTINGS.verifySig
502514
const gpgSpy = jest.spyOn(gpg, 'verify').mockResolvedValue(undefined)
503515

504516
beforeEach(() => {
@@ -507,17 +519,17 @@ describe('Test makeAvailable', () => {
507519
jest.spyOn(utils, 'extract').mockResolvedValue('/tmp/p2')
508520
jest.spyOn(tc, 'cacheDir').mockResolvedValue('/tmp/p3')
509521
})
510-
afterEach(() => SETTINGS.verify_sig = save)
522+
afterEach(() => SETTINGS.verifySig = save)
511523

512524
test('verify_sig === false', async () => {
513-
SETTINGS.verify_sig = false
525+
SETTINGS.verifySig = false
514526
const dmd = await init('dmd-2.109.1')
515527
await dmd.makeAvailable()
516528
expect(gpgSpy).not.toHaveBeenCalled()
517529
})
518530

519531
test('verify_sig === true', async () => {
520-
SETTINGS.verify_sig = true
532+
SETTINGS.verifySig = true
521533
const dmd = await init('dmd-2.109.1')
522534
await dmd.makeAvailable()
523535
expect(gpgSpy).toHaveBeenCalled()

__tests__/d-ldc.test.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as utils from '../src/utils'
33
import * as testUtils from './test-helpers.test'
44
import * as tc from '@actions/tool-cache'
55
import fs from 'fs'
6+
import SETTINGS from '../src/settings'
67

78
testUtils.saveProcessRestorePoint()
89
testUtils.disableNetwork()
@@ -460,10 +461,10 @@ test('ldc fails on unsupported platforms', async () => {
460461
describe('Test makeAvailable', () => {
461462
const root = '/tmp/cache'
462463
const origEnv = process.env
464+
const origSep = SETTINGS.sep
465+
const origExeExt = SETTINGS.exeExt
463466

464-
// These values are cached so they match the hosts
465-
const sep = (process.platform == 'win32' ? '\\' : '/')
466-
const exeExt = (process.platform == 'win32' ? '.exe' : '')
467+
// This value is cached so it matches the host's
467468
const pathSep = (process.platform == 'win32' ? ';' : ':')
468469

469470
beforeEach(() => {
@@ -473,34 +474,44 @@ describe('Test makeAvailable', () => {
473474
process.env['PATH'] = '/bin'
474475
process.env['LD_LIBRARY_PATH'] = ''
475476
})
476-
afterEach(() => process.env = origEnv)
477+
afterEach(() => {
478+
process.env = origEnv
479+
SETTINGS.sep = origSep
480+
SETTINGS.exeExt = origExeExt
481+
})
477482

478483
test('linux', async () => {
479484
Object.defineProperty(process, 'platform', { value: 'linux' })
485+
SETTINGS.sep = '/'
486+
SETTINGS.exeExt = ''
480487
const ldc = await init('ldc-1.39.0')
481488
await ldc.makeAvailable()
482489
expect(process.env['PATH']).toBe(root + '/ldc2-1.39.0-linux-x86_64/bin' + pathSep + '/bin')
483490
expect(process.env['LD_LIBRARY_PATH']).toBe(root + '/ldc2-1.39.0-linux-x86_64/lib')
484-
expect(process.env['DC']).toBe(root + `/ldc2-1.39.0-linux-x86_64/bin${sep}ldc2${exeExt}`)
485-
expect(process.env['DMD']).toBe(root + `/ldc2-1.39.0-linux-x86_64/bin${sep}ldmd2${exeExt}`)
491+
expect(process.env['DC']).toBe(root + '/ldc2-1.39.0-linux-x86_64/bin/ldc2')
492+
expect(process.env['DMD']).toBe(root + '/ldc2-1.39.0-linux-x86_64/bin/ldmd2')
486493
})
487494

488495
test('osx', async () => {
489496
Object.defineProperty(process, 'platform', { value: 'darwin' })
497+
SETTINGS.sep = '/'
498+
SETTINGS.exeExt = ''
490499
const ldc = await init('ldc-1.39.0')
491500
await ldc.makeAvailable()
492501
expect(process.env['PATH']).toBe(root + '/ldc2-1.39.0-osx-universal/bin' + pathSep + '/bin')
493502
expect(process.env['LD_LIBRARY_PATH']).toBe(root + '/ldc2-1.39.0-osx-universal/lib-x86_64' + ':' + root + '/ldc2-1.39.0-osx-universal/lib-arm64')
494-
expect(process.env['DC']).toBe(root + `/ldc2-1.39.0-osx-universal/bin${sep}ldc2${exeExt}`)
495-
expect(process.env['DMD']).toBe(root + `/ldc2-1.39.0-osx-universal/bin${sep}ldmd2${exeExt}`)
503+
expect(process.env['DC']).toBe(root + '/ldc2-1.39.0-osx-universal/bin/ldc2')
504+
expect(process.env['DMD']).toBe(root + '/ldc2-1.39.0-osx-universal/bin/ldmd2')
496505
})
497506

498507
test('windows', async () => {
499508
Object.defineProperty(process, 'platform', { value: 'win32' })
509+
SETTINGS.sep = '\\'
510+
SETTINGS.exeExt = '.exe'
500511
const ldc = await init('ldc-1.39.0')
501512
await ldc.makeAvailable()
502513
expect(process.env['PATH']).toBe(root + '\\ldc2-1.39.0-windows-multilib\\lib64' + pathSep + root + '\\ldc2-1.39.0-windows-multilib\\bin' + pathSep + '/bin')
503-
expect(process.env['DC']).toBe(root + `\\ldc2-1.39.0-windows-multilib\\bin${sep}ldc2${exeExt}`)
504-
expect(process.env['DMD']).toBe(root + `\\ldc2-1.39.0-windows-multilib\\bin${sep}ldmd2${exeExt}`)
514+
expect(process.env['DC']).toBe(root + '\\ldc2-1.39.0-windows-multilib\\bin\\ldc2.exe')
515+
expect(process.env['DMD']).toBe(root + '\\ldc2-1.39.0-windows-multilib\\bin\\ldmd2.exe')
505516
})
506517
})

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/d.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import * as fs from 'fs';
66
import * as semver from './semver'
77
import * as exec from '@actions/exec'
88

9-
const sep = (process.platform == 'win32' ? '\\' : '/')
10-
const exeExt = (process.platform == 'win32' ? '.exe' : '')
11-
12-
export const SETTINGS = {
13-
verify_sig: core.getInput('verify_sig') !== 'false'
14-
}
9+
import SETTINGS from './settings'
1510

1611
/** Base interface for all D tools */
1712
export interface ITool {
@@ -105,7 +100,7 @@ export class Compiler implements ITool {
105100
} else {
106101
console.log(`Downloading ${this.url}`);
107102
const archive = await utils.downloadTool(this.url)
108-
if (SETTINGS.verify_sig && this.sig) {
103+
if (SETTINGS.verifySig && this.sig) {
109104
console.log("Verifying the download with GPG");
110105
await gpg.verify(archive, this.sig);
111106
}
@@ -118,8 +113,8 @@ export class Compiler implements ITool {
118113

119114
/** Set the DC and DMD environment variable to point to the newly extracted compiler */
120115
setDC(root: string) {
121-
core.exportVariable("DC", root + this.binPath + sep + this.name + exeExt)
122-
core.exportVariable("DMD", root + this.binPath + sep + this.dmdWrapperExeName + exeExt)
116+
core.exportVariable("DC", root + this.binPath + SETTINGS.sep + this.name + SETTINGS.exeExt)
117+
core.exportVariable("DMD", root + this.binPath + SETTINGS.sep + this.dmdWrapperExeName + SETTINGS.exeExt)
123118
}
124119

125120
/** Take all the necessary steps to make the compiler available on the host
@@ -675,7 +670,7 @@ export class LDC extends Compiler {
675670

676671
export class Dub implements ITool {
677672
public readonly name = 'dub'
678-
public readonly exeName = this.name + exeExt
673+
public readonly exeName = this.name + SETTINGS.exeExt
679674
constructor(public url: string, public version: string) {}
680675

681676
/** Parse a version string and compute the associated version
@@ -750,7 +745,7 @@ export class Dub implements ITool {
750745
if (!cached) {
751746
const archive = await utils.downloadTool(this.url)
752747
let extracted = await utils.extract(this.url, archive)
753-
cached = await tc.cacheFile(extracted + sep + this.exeName,
748+
cached = await tc.cacheFile(extracted + SETTINGS.sep + this.exeName,
754749
this.exeName, this.name, this.version)
755750
}
756751
return cached

src/settings.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as core from '@actions/core'
2+
3+
const SETTINGS = {
4+
verifySig: core.getInput('verify_sig') !== 'false',
5+
6+
sep: process.platform == 'win32' ? '\\' : '/',
7+
exeExt: process.platform == 'win32' ? '.exe' : '',
8+
}
9+
10+
export default SETTINGS

0 commit comments

Comments
 (0)