From 40a00ae373c82fa772048a64b88eeb4cb2331a63 Mon Sep 17 00:00:00 2001
From: Danny McCormick <damccorm@microsoft.com>
Date: Tue, 6 Aug 2019 15:27:51 -0400
Subject: [PATCH] Add tests

---
 __tests__/__snapshots__/authutil.test.ts.snap | 21 +++++++
 __tests__/authutil.test.ts                    | 62 +++++++++++++++++++
 __tests__/installer.test.ts                   | 13 +---
 3 files changed, 85 insertions(+), 11 deletions(-)
 create mode 100644 __tests__/__snapshots__/authutil.test.ts.snap
 create mode 100644 __tests__/authutil.test.ts

diff --git a/__tests__/__snapshots__/authutil.test.ts.snap b/__tests__/__snapshots__/authutil.test.ts.snap
new file mode 100644
index 0000000..99174a6
--- /dev/null
+++ b/__tests__/__snapshots__/authutil.test.ts.snap
@@ -0,0 +1,21 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`installer tests Appends trailing slash to registry 1`] = `
+"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
+registry=https://registry.npmjs.org/"
+`;
+
+exports[`installer tests Automatically configures GPR scope 1`] = `
+"npm.pkg.github.com/:_authToken=\${NODE_AUTH_TOKEN}
+@owner:registry=npm.pkg.github.com/"
+`;
+
+exports[`installer tests Configures scoped npm registries 1`] = `
+"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
+@myScope:registry=https://registry.npmjs.org/"
+`;
+
+exports[`installer tests Sets up npmrc for npmjs 1`] = `
+"//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}
+registry=https://registry.npmjs.org/"
+`;
diff --git a/__tests__/authutil.test.ts b/__tests__/authutil.test.ts
new file mode 100644
index 0000000..3dab907
--- /dev/null
+++ b/__tests__/authutil.test.ts
@@ -0,0 +1,62 @@
+import io = require('@actions/io');
+import fs = require('fs');
+import path = require('path');
+
+const tempDir = path.join(
+  __dirname,
+  'runner',
+  path.join(
+    Math.random()
+      .toString(36)
+      .substring(7)
+  ),
+  'temp'
+);
+
+const rcFile = path.join(tempDir, '.npmrc');
+
+process.env['GITHUB_REPOSITORY'] = 'owner/repo';
+process.env['RUNNER_TEMP'] = tempDir;
+import * as auth from '../src/authutil';
+
+describe('installer tests', () => {
+  beforeAll(async () => {
+    await io.rmRF(tempDir);
+    await io.mkdirP(tempDir);
+  }, 100000);
+
+  beforeEach(() => {
+    if (fs.existsSync(rcFile)) {
+      fs.unlinkSync(rcFile);
+    }
+    process.env['INPUT_SCOPE'] = '';
+  });
+
+  it('Sets up npmrc for npmjs', async () => {
+    await auth.configAuthentication('https://registry.npmjs.org/');
+    expect(fs.existsSync(rcFile)).toBe(true);
+    expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
+  });
+
+  it('Appends trailing slash to registry', async () => {
+    await auth.configAuthentication('https://registry.npmjs.org');
+
+    expect(fs.existsSync(rcFile)).toBe(true);
+    expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
+  });
+
+  it('Configures scoped npm registries', async () => {
+    process.env['INPUT_SCOPE'] = 'myScope';
+    await auth.configAuthentication('https://registry.npmjs.org');
+
+    expect(fs.existsSync(rcFile)).toBe(true);
+    expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
+  });
+
+  it('Automatically configures GPR scope', async () => {
+    await auth.configAuthentication('npm.pkg.github.com');
+
+    expect(fs.existsSync(rcFile)).toBe(true);
+    expect(fs.readFileSync(rcFile, {encoding: 'utf8'})).toMatchSnapshot();
+  });
+});
diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts
index e2aa653..e0ada32 100644
--- a/__tests__/installer.test.ts
+++ b/__tests__/installer.test.ts
@@ -4,7 +4,7 @@ import os = require('os');
 import path = require('path');
 
 const toolDir = path.join(
-  process.cwd(),
+  __dirname,
   'runner',
   path.join(
     Math.random()
@@ -14,7 +14,7 @@ const toolDir = path.join(
   'tools'
 );
 const tempDir = path.join(
-  process.cwd(),
+  __dirname,
   'runner',
   path.join(
     Math.random()
@@ -36,15 +36,6 @@ describe('installer tests', () => {
     await io.rmRF(tempDir);
   }, 100000);
 
-  afterAll(async () => {
-    try {
-      await io.rmRF(toolDir);
-      await io.rmRF(tempDir);
-    } catch {
-      console.log('Failed to remove test directories');
-    }
-  }, 100000);
-
   it('Acquires version of node if no matching version is installed', async () => {
     await installer.getNode('10.16.0');
     const nodeDir = path.join(toolDir, 'node', '10.16.0', os.arch());