tests: clean up modcache after tests

Run `go clean -modcache` after setting up a temporary testing GOPATH.

Change-Id: I7bf7a17f626c8a004475555c14a6b1668eede229
GitHub-Last-Rev: e776573051c1a4a4f543701d220675f795cd5139
GitHub-Pull-Request: golang/vscode-go#67
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/234357
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/test/integration/install.test.ts b/test/integration/install.test.ts
index 778e5f2..4c73e47 100644
--- a/test/integration/install.test.ts
+++ b/test/integration/install.test.ts
@@ -5,6 +5,7 @@
 
 import AdmZip = require('adm-zip');
 import * as assert from 'assert';
+import cp = require('child_process');
 import fs = require('fs');
 import os = require('os');
 import path = require('path');
@@ -13,7 +14,7 @@
 import vscode = require('vscode');
 import { installTools } from '../../src/goInstallTools';
 import { getTool, getToolAtVersion } from '../../src/goTools';
-import { getGoVersion, rmdirRecursive } from '../../src/util';
+import { getBinPath, getGoVersion, rmdirRecursive } from '../../src/util';
 
 suite('Installation Tests', () => {
 	test('install tools', async () => {
@@ -56,10 +57,15 @@
 			const files = await readdir(path.join(tmpToolsGopath, 'bin'));
 			assert.deepEqual(files, missing, `tool installation failed for ${missing}`);
 
-			// TODO(rstambler): A module cache gets created in $GOPATH/pkg with
-			// different permissions, and fs.chown doesn't seem to work on it.
-			// Not sure how to remove the files so that the temporary directory
-			// can be deleted.
+			// Clean up the temporary GOPATH. To delete the module cache, run `go clean -modcache`.
+			const goRuntimePath = getBinPath('go');
+			const envForTest = Object.assign({}, process.env);
+			envForTest['GOPATH'] = tmpToolsGopath;
+			const execFile = util.promisify(cp.execFile);
+			await execFile(goRuntimePath, ['clean', '-modcache'], {
+				env: envForTest,
+			});
+			rmdirRecursive(tmpToolsGopath);
 		}
 
 		rmdirRecursive(proxyDir);