Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Add tests back
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Nov 6, 2017
1 parent 9559b2c commit eb8bfdb
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/goPlayground.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import vscode = require('vscode');
import { stat } from 'fs';
import * as path from 'path';
import { execFile } from 'child_process';
import { outputChannel } from './goStatus';
import { getBinPath } from './util';
Expand All @@ -15,34 +15,47 @@ export const playgroundCommand = () => {
vscode.window.showInformationMessage('No editor is active.');
return;
}
const config: flags = vscode.workspace.getConfiguration('go', editor.document.uri).get('playground');

const selection = editor.selection;
const code = selection.isEmpty
? editor.document.getText()
: editor.document.getText(selection);
const binaryLocation = getBinPath(TOOL_CMD_NAME);
if (!path.isAbsolute(binaryLocation)) {
return promptForMissingTool(TOOL_CMD_NAME);;
}

outputChannel.clear();
outputChannel.show();
outputChannel.appendLine('Upload to the Go Playground in progress...\n');

const binaryLocation = getBinPath(TOOL_CMD_NAME);
stat(binaryLocation, (err, stats) => {
if (err || !stats.isFile()) {
promptForMissingTool(TOOL_CMD_NAME);
return;
const selection = editor.selection;
const code = selection.isEmpty
? editor.document.getText()
: editor.document.getText(selection);
goPlay(code, vscode.workspace.getConfiguration('go', editor.document.uri).get('playground')).then(result => {
outputChannel.append(result);
}, (e: string) => {
if (e) {
vscode.window.showErrorMessage(e);
}
const cliArgs = Object.keys(config).map(key => `-${key}=${config[key]}`);
});
};

export function goPlay(code: string, goConfig: vscode.WorkspaceConfiguration): Thenable<string> {
const cliArgs = Object.keys(goConfig).map(key => `-${key}=${goConfig[key]}`);
const binaryLocation = getBinPath(TOOL_CMD_NAME);

return new Promise<string>((resolve, reject) => {
execFile(binaryLocation, [...cliArgs, '-'], (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool(TOOL_CMD_NAME);
return reject();
}
if (err) {
vscode.window.showErrorMessage(`${TOOL_CMD_NAME}: ${stdout || stderr || err.message}`);
return;
return reject(`${TOOL_CMD_NAME}: ${stdout || stderr || err.message}`);
}
outputChannel.append(
`Output from the Go Playground:
return resolve(
`Output from the Go Playground:
${stdout || stderr}
Finished running tool: ${binaryLocation} ${cliArgs.join(' ')} -\n`
);
}).stdin.end(code);
});
};
}
72 changes: 72 additions & 0 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { listPackages } from '../src/goImport';
import { generateTestCurrentFile, generateTestCurrentPackage, generateTestCurrentFunction } from '../src/goGenerateTests';
import { getAllPackages } from '../src/goPackages';
import { getImportPath } from '../src/util';
import { goPlay } from '../src/goPlayground';

suite('Go Extension Tests', () => {
let gopath = process.env['GOPATH'];
Expand Down Expand Up @@ -756,4 +757,75 @@ It returns the number of bytes written and any write error encountered.
assert.equal(run[1], getImportPath(run[0]));
});
});

test('goPlay - success run', (done) => {
const validCode = `
package main
import (
"fmt"
)
func main() {
for i := 1; i < 4; i++ {
fmt.Printf("%v ", i)
}
fmt.Print("Go!")
}`;
const goConfig = Object.create(vscode.workspace.getConfiguration('go'), {
'playground': { value: { run: true, openbrowser: false, share: false } }
});

goPlay(validCode, goConfig['playground']).then(result => {
assert(
result.includes('1 2 3 Go!')
);
}, (e) => {
assert.ifError(e);
}).then(() => done(), done);
});


test('goPlay - success run & share', (done) => {
const validCode = `
package main
import (
"fmt"
)
func main() {
for i := 1; i < 4; i++ {
fmt.Printf("%v ", i)
}
fmt.Print("Go!")
}`;
const goConfig = Object.create(vscode.workspace.getConfiguration('go'), {
'playground': { value: { run: true, openbrowser: false, share: true } }
});

goPlay(validCode, goConfig['playground']).then(result => {
assert(result.includes('1 2 3 Go!'));
assert(result.includes('https://play.golang.org/'));
}, (e) => {
assert.ifError(e);
}).then(() => done(), done);
});

test('goPlay - fail', (done) => {
const invalidCode = `
package main
import (
"fmt"
)
func fantasy() {
fmt.Print("not a main package, sorry")
}`;
const goConfig = Object.create(vscode.workspace.getConfiguration('go'), {
'playground': { value: { run: true, openbrowser: false, share: false } }
});

goPlay(invalidCode, goConfig['playground']).then(result => {
assert.ifError(result);
}, (e) => {
assert.ok(e);
}).then(() => done(), done);
});

});

0 comments on commit eb8bfdb

Please sign in to comment.