From 0001c12c91760363ad8342b1f2a298faebc7cce9 Mon Sep 17 00:00:00 2001 From: Othmane Kinane <76955412+othmane-kinane-nw@users.noreply.github.com> Date: Sat, 11 Mar 2023 00:06:42 +0100 Subject: [PATCH] add unit test --- __tests__/setup-dotnet.test.ts | 36 ++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/__tests__/setup-dotnet.test.ts b/__tests__/setup-dotnet.test.ts index bf39881..76c1da4 100644 --- a/__tests__/setup-dotnet.test.ts +++ b/__tests__/setup-dotnet.test.ts @@ -18,6 +18,15 @@ if (IS_WINDOWS) { toolDir = path.join(process.env['HOME'] + '', '.dotnet'); } +function createGlobalJsonPath(dotnetVersion: string) { + const globalJsonPath = path.join(process.cwd(), 'global.json'); + const jsonContents = `{${os.EOL}"sdk": {${os.EOL}"version": "${dotnetVersion}"${os.EOL}}${os.EOL}}`; + if (!fs.existsSync(globalJsonPath)) { + fs.writeFileSync(globalJsonPath, jsonContents); + } + return globalJsonPath; +} + const tempDir = path.join(__dirname, 'runner', 'temp2'); describe('setup-dotnet tests', () => { @@ -52,11 +61,7 @@ describe('setup-dotnet tests', () => { }, 30000); it('Acquires version of dotnet from global.json if no matching version is installed', async () => { - const globalJsonPath = path.join(process.cwd(), 'global.json'); - const jsonContents = `{${os.EOL}"sdk": {${os.EOL}"version": "3.1.201"${os.EOL}}${os.EOL}}`; - if (!fs.existsSync(globalJsonPath)) { - fs.writeFileSync(globalJsonPath, jsonContents); - } + createGlobalJsonPath('3.1.201'); await setup.run(); expect(fs.existsSync(path.join(toolDir, 'sdk', '3.1.201'))).toBe(true); @@ -78,11 +83,7 @@ describe('setup-dotnet tests', () => { }, 400000); it("Sets output with the version specified in global.json, if it's present", async () => { - const globalJsonPath = path.join(process.cwd(), 'global.json'); - const jsonContents = `{${os.EOL}"sdk": {${os.EOL}"version": "3.0.103"${os.EOL}}${os.EOL}}`; - if (!fs.existsSync(globalJsonPath)) { - fs.writeFileSync(globalJsonPath, jsonContents); - } + createGlobalJsonPath('3.0.103'); inputs['dotnet-version'] = ['3.1.201', '6.0.401']; inputs['global-json-file'] = './global.json'; @@ -95,4 +96,19 @@ describe('setup-dotnet tests', () => { expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '3.0.103'); }, 400000); + + it('Sets output with the version specified in global.json with absolute path', async () => { + const globalJsonPath = createGlobalJsonPath('3.0.103'); + + inputs['dotnet-version'] = ['3.1.201', '6.0.401']; + inputs['global-json-file'] = globalJsonPath; + + getMultilineInputSpy.mockImplementation(input => inputs[input]); + + getInputSpy.mockImplementation(input => inputs[input]); + + await setup.run(); + + expect(setOutputSpy).toHaveBeenCalledWith('dotnet-version', '3.0.103'); + }, 400000); });