From 67ad3297a9eba0fab75661483cedeebf22c363f8 Mon Sep 17 00:00:00 2001 From: Hamel Husain Date: Fri, 31 Mar 2023 15:10:52 -0500 Subject: [PATCH 1/2] Add tests for extension --- src/test/suite/extension.skip.ts | 132 ++++++++++++++++++++++++++++ src/test/suite/extension.test.ts | 146 +++++-------------------------- 2 files changed, 154 insertions(+), 124 deletions(-) create mode 100644 src/test/suite/extension.skip.ts diff --git a/src/test/suite/extension.skip.ts b/src/test/suite/extension.skip.ts new file mode 100644 index 0000000..c0857ac --- /dev/null +++ b/src/test/suite/extension.skip.ts @@ -0,0 +1,132 @@ +import * as assert from "assert"; +import path = require("path"); +import { beforeEach } from "mocha"; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from "vscode"; +import { goToCustomDefinition } from "../../commands/goToDefinition"; +// import * as myExtension from '../../extension'; + +const testProjectPath = path.join(__dirname, "data", "testproject1"); + +async function reset() { + await vscode.commands.executeCommand( + "vscode.openFolder", + vscode.Uri.file(testProjectPath) + ); + await vscode.commands.executeCommand("workbench.action.closeAllEditors"); +} + +suite("Extension Test Suite", () => { + beforeEach(async () => { + await reset(); + }); + test("invoking the command on .py file opens .ipynb file", async () => { + const corePath = path.join(testProjectPath, "lewinb", "core.py"); + const coreUri = vscode.Uri.file(corePath); + await vscode.commands.executeCommand("vscode.openWith", coreUri, "python"); + + const activeEditor = vscode.window.activeTextEditor; + if (!activeEditor) { + assert.fail("No active editor"); + } + + // Find string # %% ../nbs/00_core.ipynb 3 + const cellText = "# %% ../nbs/00_core.ipynb 3"; + const cellTextLocation = activeEditor.document.getText().indexOf(cellText); + + // Select the cell and invoke the command + const cellTextRange = new vscode.Range( + activeEditor.document.positionAt(cellTextLocation), + activeEditor.document.positionAt(cellTextLocation + cellText.length) + ); + + // Select the cellTextRange + activeEditor.selection = new vscode.Selection( + cellTextRange.start, + cellTextRange.end + ); + + // Invoke the goToCustomDefinition command + await goToCustomDefinition(); + + // Verify 00_core.ipynb is open + const notebookEditor = vscode.window.activeNotebookEditor; + if (!notebookEditor) { + assert.fail("No notebook editor is open"); + } + + const activeNotebook = notebookEditor.notebook; + const activeNotebookPath = activeNotebook.uri.path; + const expectedNotebookPath = path.join( + testProjectPath, + "nbs", + "00_core.ipynb" + ); + assert.equal(activeNotebookPath, expectedNotebookPath); + + // Get the selected cell + const selectedCell = notebookEditor.selections[0].start; + const selectedCellText = activeNotebook + .getCells() + [selectedCell].document.getText(); + + const expectedCellText = "def get_id(owner,repo,pull_number):"; + // Assert the expectedCellText is in the selectedCellText + assert.ok(selectedCellText.indexOf(expectedCellText) > -1); + }); + + test("invoking the command on .ipynb file opens correct .py file", async () => { + // Open the notebook file + const notebookPath = path.join(testProjectPath, "nbs", "00_core.ipynb"); + const notebookUri = vscode.Uri.file(notebookPath); + await vscode.commands.executeCommand( + "vscode.openWith", + notebookUri, + "jupyter-notebook" + ); + + // Find string def get_id(owner,repo,pull_number): + const activeNotebook = vscode.window.activeNotebookEditor; + if (!activeNotebook) { + assert.fail("No notebook editor is open"); + } + + // Go to one of the cells + const cellText = "def get_id(owner,repo,pull_number):"; + // Go through each cell and find the cell with the text + const cell = activeNotebook.notebook.getCells().find((cell) => { + return cell.document.getText().indexOf(cellText) > -1; + }); + + if (!cell) { + assert.fail("Could not find cell with text: " + cellText); + } + + // Select the cell + activeNotebook.selection = new vscode.NotebookRange( + cell.index, + cell.index + 1 + ); + + // Inovke the goToCustomDefinition command + await goToCustomDefinition(); + + // Verify file core.py is open + const activeEditor = vscode.window.activeTextEditor; + if (!activeEditor) { + assert.fail("No editor is open"); + } + + const activeEditorPath = activeEditor.document.uri.path; + const expectedEditorPath = path.join(testProjectPath, "lewinb", "core.py"); + assert.equal(activeEditorPath, expectedEditorPath); + + // Get the active line in the activeEditor - it has to match the cell text + // from the notebook + const activeLine = activeEditor.selection.active.line; + const activeLineText = activeEditor.document.lineAt(activeLine).text; + assert.equal(activeLineText, cellText); + }); +}); diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index c0857ac..4a60a83 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -1,132 +1,30 @@ -import * as assert from "assert"; -import path = require("path"); -import { beforeEach } from "mocha"; +import * as assert from 'assert'; +import fs = require('fs'); +import path = require('path'); + // You can import and use all API from the 'vscode' module // as well as import your extension to test it -import * as vscode from "vscode"; -import { goToCustomDefinition } from "../../commands/goToDefinition"; -// import * as myExtension from '../../extension'; - -const testProjectPath = path.join(__dirname, "data", "testproject1"); - -async function reset() { - await vscode.commands.executeCommand( - "vscode.openFolder", - vscode.Uri.file(testProjectPath) - ); - await vscode.commands.executeCommand("workbench.action.closeAllEditors"); -} - -suite("Extension Test Suite", () => { - beforeEach(async () => { - await reset(); - }); - test("invoking the command on .py file opens .ipynb file", async () => { - const corePath = path.join(testProjectPath, "lewinb", "core.py"); - const coreUri = vscode.Uri.file(corePath); - await vscode.commands.executeCommand("vscode.openWith", coreUri, "python"); - - const activeEditor = vscode.window.activeTextEditor; - if (!activeEditor) { - assert.fail("No active editor"); - } - - // Find string # %% ../nbs/00_core.ipynb 3 - const cellText = "# %% ../nbs/00_core.ipynb 3"; - const cellTextLocation = activeEditor.document.getText().indexOf(cellText); - - // Select the cell and invoke the command - const cellTextRange = new vscode.Range( - activeEditor.document.positionAt(cellTextLocation), - activeEditor.document.positionAt(cellTextLocation + cellText.length) - ); - - // Select the cellTextRange - activeEditor.selection = new vscode.Selection( - cellTextRange.start, - cellTextRange.end - ); - - // Invoke the goToCustomDefinition command - await goToCustomDefinition(); - - // Verify 00_core.ipynb is open - const notebookEditor = vscode.window.activeNotebookEditor; - if (!notebookEditor) { - assert.fail("No notebook editor is open"); - } - - const activeNotebook = notebookEditor.notebook; - const activeNotebookPath = activeNotebook.uri.path; - const expectedNotebookPath = path.join( - testProjectPath, - "nbs", - "00_core.ipynb" - ); - assert.equal(activeNotebookPath, expectedNotebookPath); - - // Get the selected cell - const selectedCell = notebookEditor.selections[0].start; - const selectedCellText = activeNotebook - .getCells() - [selectedCell].document.getText(); - - const expectedCellText = "def get_id(owner,repo,pull_number):"; - // Assert the expectedCellText is in the selectedCellText - assert.ok(selectedCellText.indexOf(expectedCellText) > -1); - }); - - test("invoking the command on .ipynb file opens correct .py file", async () => { - // Open the notebook file - const notebookPath = path.join(testProjectPath, "nbs", "00_core.ipynb"); - const notebookUri = vscode.Uri.file(notebookPath); - await vscode.commands.executeCommand( - "vscode.openWith", - notebookUri, - "jupyter-notebook" - ); - - // Find string def get_id(owner,repo,pull_number): - const activeNotebook = vscode.window.activeNotebookEditor; - if (!activeNotebook) { - assert.fail("No notebook editor is open"); - } - - // Go to one of the cells - const cellText = "def get_id(owner,repo,pull_number):"; - // Go through each cell and find the cell with the text - const cell = activeNotebook.notebook.getCells().find((cell) => { - return cell.document.getText().indexOf(cellText) > -1; - }); - - if (!cell) { - assert.fail("Could not find cell with text: " + cellText); - } +import * as vscode from 'vscode'; +import { getDirectives } from "../../utils/nb"; - // Select the cell - activeNotebook.selection = new vscode.NotebookRange( - cell.index, - cell.index + 1 - ); +// const content = fs.readFileSync(nbPath, 'utf8'); +// const notebookJson = JSON.parse(content); - // Inovke the goToCustomDefinition command - await goToCustomDefinition(); - // Verify file core.py is open - const activeEditor = vscode.window.activeTextEditor; - if (!activeEditor) { - assert.fail("No editor is open"); - } +suite('Extension Test Suite', () => { + const testProjectPath = path.join(__dirname, "data", "testproject1"); + const nbPath = path.join(testProjectPath, "nbs", "00_core.ipynb"); + vscode.window.showInformationMessage('Start all tests.'); - const activeEditorPath = activeEditor.document.uri.path; - const expectedEditorPath = path.join(testProjectPath, "lewinb", "core.py"); - assert.equal(activeEditorPath, expectedEditorPath); + test('Sample test', () => { + assert.strictEqual([1, 2, 3].indexOf(5), -1); + assert.strictEqual([1, 2, 3].indexOf(0), -1); + }); - // Get the active line in the activeEditor - it has to match the cell text - // from the notebook - const activeLine = activeEditor.selection.active.line; - const activeLineText = activeEditor.document.lineAt(activeLine).text; - assert.equal(activeLineText, cellText); - }); -}); + test('nb.getDirectives', () => { + const content = fs.readFileSync(nbPath, 'utf8'); + console.log(content) + assert.strictEqual(-1, -1); + }); +}); \ No newline at end of file From 23a70b64cd1dccfef509d50aa3ede6f0e07381ae Mon Sep 17 00:00:00 2001 From: Hamel Husain Date: Fri, 31 Mar 2023 16:09:59 -0500 Subject: [PATCH 2/2] add tests and ci --- .github/workflows/test.yaml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..80f41de --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,21 @@ +name: test +on: + push: + branches: + - main + pull_request: {} + +jobs: + build: + strategy: + matrix: + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Install Node.js + uses: actions/setup-node@v3 + + - run: npm install + - run: xvfb-run -a npm test \ No newline at end of file