Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing and final release v1 #35

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "playground"]
path = playground
url = https://github.com/open-southeners/vscode-laravel-pint-playground.git
8 changes: 6 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@
"type": "extensionHost",
"request": "launch",
"args": [
"--skip-welcome",
"--disable-extensions",
"--skip-release-notes",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index",
"${workspaceFolder}/playground/laravel"
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
]
}
]
}
}
18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,22 @@
"@types/glob": "^8.0.0",
"@types/mocha": "^9.1.1",
"@types/node": "14.x",
"@types/tmp": "^0.2.3",
"@types/vscode": "^1.68.0",
"@typescript-eslint/eslint-plugin": "^5.12.1",
"@typescript-eslint/parser": "^5.12.1",
"@vscode/test-electron": "^2.1.2",
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"@vscode/test-electron": "^2.2.2",
"@vscode/vsce": "^2.16.0",
"command-exists": "^1.2.9",
"esbuild": "^0.15.15",
"eslint": "^8.28.0",
"esbuild": "^0.17.0",
"eslint": "^8.31.0",
"fs-extra": "^10.1.0",
"glob": "^8.0.3",
"mocha": "^9.2.2",
"node-fetch": "^3.3.0",
"ovsx": "^0.5.2",
"typescript": "^4.9.3",
"vsce": "^2.14.0"
"ovsx": "^0.8.0",
"tmp": "^0.2.1",
"typescript": "^4.9.4"
},
"engines": {
"vscode": "^1.68.0"
Expand Down
1 change: 1 addition & 0 deletions playground
Submodule playground added at ae84e2
6 changes: 0 additions & 6 deletions playground/index.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { window } from "vscode";
import PintEditService from "./PintEditService";

export function formatCommand(pintEditService: PintEditService) {
export async function formatCommand(pintEditService: PintEditService) {
const activeTextEditorDocumentUri = window.activeTextEditor?.document.uri;

if (!activeTextEditorDocumentUri) {
return;
}

pintEditService.formatFile(activeTextEditorDocumentUri);
return await pintEditService.formatFile(activeTextEditorDocumentUri);
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function activate(context: ExtensionContext) {
}
);

const formatFileCommand = commands.registerCommand('laravel-pint.format', () => formatCommand(editService));
const formatFileCommand = commands.registerCommand('laravel-pint.format', async () => await formatCommand(editService));
const formatProjectCommand = commands.registerCommand('laravel-pint.formatProject', () => editService.formatWorkspace());

context.subscriptions.push(
Expand Down
57 changes: 40 additions & 17 deletions src/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
import * as path from 'path';

import { runTests } from '@vscode/test-electron';
import * as tmp from 'tmp';
import * as fs from 'fs-extra';

async function createTempDir() {
return new Promise<string>((resolve, reject) => {
tmp.dir((err, dir) => {
if (err) {
return reject(err);
}
resolve(dir);
});
});
}

async function createSettings(): Promise<string> {
const userDataDirectory = await createTempDir();
process.env.VSC_JUPYTER_VSCODE_SETTINGS_DIR = userDataDirectory;
const settingsFile = path.join(userDataDirectory, "User", "settings.json");
const defaultSettings: Record<string, string | boolean | string[]> = {
"laravel-pint.enable": true,
"editor.defaultFormatter": "open-southeners.laravel-pint",
"laravel-pint.enableDebugLogs": true,
"security.workspace.trust.enabled": false, // Disable trusted workspaces.
};

fs.ensureDirSync(path.dirname(settingsFile));
fs.writeFileSync(settingsFile, JSON.stringify(defaultSettings, undefined, 4));
return userDataDirectory;
}

async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');

// The path to test runner
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');

// Download VS Code, unzip it and run the integration test
const workspacePath = path.resolve(__dirname, '../../playground/laravel');

const userDataDirectory = await createSettings();

await runTests({
extensionDevelopmentPath,
extensionTestsPath,
/**
* A list of launch arguments passed to VS Code executable, in addition to `--extensionDevelopmentPath`
* and `--extensionTestsPath` which are provided by `extensionDevelopmentPath` and `extensionTestsPath`
* options.
*
* If the first argument is a path to a file/folder/workspace, the launched VS Code instance
* will open it.
*
* See `code --help` for possible arguments.
*/
launchArgs: [path.resolve(__dirname, '../../playground'), '--disable-extensions']
launchArgs: [workspacePath]
.concat(["--wait"])
.concat(["--skip-welcome"])
.concat(["--disable-extensions"])
.concat(["--skip-release-notes"])
.concat(["--enable-proposed-api"])
.concat(["--user-data-dir", userDataDirectory]),
});


Expand Down
83 changes: 41 additions & 42 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
import * as assert from 'assert';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import { afterEach } from 'mocha';
import * as fs from 'fs';
import { beforeEach } from 'mocha';
import path = require('path');
import * as vscode from 'vscode';
import * as myExtension from '../../extension';
import { PresetOptions } from '../../types';
import { asAbsolutePathFromWorkspaceFolder, buildCommandFromConfig } from '../../util';

suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

// TODO: Not there yet...
// myExtension.activate();
let originalFile = {
path: '',
content: ''
};

beforeEach(() => {
const workspaceConfigFile = path.resolve(__dirname, '../../../plaground/.vscode/settings.json');
afterEach(async () => {
if (originalFile.path !== '') {
fs.writeFileSync(originalFile.path, originalFile.content, { encoding: 'utf-8' });
}
});

if (fs.existsSync(workspaceConfigFile)) {
fs.rmSync(workspaceConfigFile);
suite('Extension Test Suite', () => {
test('Open messy file and run formatting over it', async () => {
await vscode.extensions.getExtension('open-southeners.laravel-pint')!.activate();

const foundFiles = await vscode.workspace.findFiles("config/vendor.php");
const document = await vscode.workspace.openTextDocument(foundFiles[0]);

await vscode.languages.setTextDocumentLanguage(document, "php");
try {
await vscode.window.showTextDocument(document);
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
throw error;
}
});

test('Default config settings', () => {
const config = vscode.workspace.getConfiguration('laravel-pint');

assert.strictEqual(config.get<string>('configPath'), '');
assert.strictEqual(config.get<string>('executablePath'), '');
assert.strictEqual(config.get<boolean>('formatOnSave'), true);
assert.strictEqual(config.get<PresetOptions>('preset'), 'auto');
assert.strictEqual(config.get<boolean>('runInLaravelSail'), false);
assert.strictEqual(config.get<string>('sailExecutablePath'), '');
});

test('Build command with config args', async () => {
const config = vscode.workspace.getConfiguration('laravel-pint');

await config.update('executablePath', 'pint');
await config.update('configPath', 'mypintconfig.json');

const cmd = await buildCommandFromConfig('index.php', config);

console.log(cmd);

assert.ok(cmd);
assert.ok(cmd.filter(value => ['vendor/bin/pint', 'index.php', '--config', 'mypintconfig.json'].includes(value)).length > 1);
});

const oldText = document.getText();

originalFile.path = document.uri.path;
originalFile.content = oldText;

console.time('formatting file with Pint');

await vscode.commands.executeCommand("laravel-pint.format");

console.timeEnd('formatting file with Pint');

await wait(1000);

assert.notStrictEqual(oldText, document.getText());
}).timeout(5000);
});
Loading