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

chore(infra): move tests folder to top level #89

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
node-version-file: '.nvmrc'
- name: Install sg
run: npm install -g @ast-grep/cli
- name: Install deps
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 16
node-version-file: '.nvmrc'
- name: Install deps
run: npm install
- name: Install vsce
Expand Down
9 changes: 5 additions & 4 deletions scripts/test/integration-test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const path = require('node:path')
const Mocha = require('mocha')
const glob = require('glob')

const TESTS_ROOT = path.join(__dirname, '../../tests')

async function run() {
await import('tsx')
// Create the mocha test
Expand All @@ -13,7 +15,7 @@ async function run() {
})

/**
Before any other tests, run the testSetup.ts file
Before any other tests, run the testSetup.ts file
This will set up the test environment necessary for the other tests to run
*/
mocha.addFile(path.join(__dirname, 'testSetup.ts'))
Expand All @@ -25,14 +27,13 @@ async function run() {
* So you could play with sort order on the tests to investigate
*/
try {
const testsRoot = path.join(__dirname, '../../src/test')
let files = await glob('**.test.ts', { cwd: testsRoot })
let files = await glob('**.test.ts', { cwd: TESTS_ROOT })
// Add files to the test suite
files.sort((a, b) => {
return a.localeCompare(b)
})
files.forEach(f => {
mocha.addFile(path.resolve(testsRoot, f))
mocha.addFile(path.resolve(TESTS_ROOT, f))
})

return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/test/testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { activate } from '../../src/test/utils'
import { activate } from '../../tests/utils'
/**
* This initialization runs once, before the rest of the tests.
* It is used to open the test fixture folder and activate the extension.
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ async function watchEditsToRestartLanguageClient(context: ExtensionContext) {
try {
// get the absolute URI to this relative path
let configUri = Uri.file(
workspace.workspaceFolders![0].uri.fsPath + '/' + configPath
path.join(workspace.workspaceFolders![0].uri.fsPath, configPath)
)
// read the config file
let configText = await workspace.fs.readFile(configUri)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode'
import * as yaml from 'js-yaml'
import path from 'path'

import {
getDocUri,
Expand All @@ -8,16 +9,19 @@ import {
assertDiagnosticsEqual
} from './utils'

const MAX_WAIT_TIME_FOR_UPDATE = 5000 // ms
const MAX_WAIT_TIME_FOR_INITIAL_DIAGNOSTICS = 10000 // ms
const MAX_WAIT_TIME_FOR_UPDATE = 10000 // ms
const MAX_WAIT_TIME_FOR_INITIAL_DIAGNOSTICS = 15000 // ms

const docUri = getDocUri('test.ts')
const diagnosticss = getExpectedDiagnosticss()
const diagnostics = getExpectedDiagnostics()

async function writeNewRule() {
let vscodeuri = vscode.Uri.file(
vscode.workspace.workspaceFolders![0].uri.fsPath +
'/rules/no-math-random.yml'
path.join(
vscode.workspace.workspaceFolders![0].uri.fsPath,
'rules',
'no-math-random.yml'
)
)
await vscode.workspace.fs.writeFile(
vscodeuri,
Expand All @@ -34,14 +38,17 @@ note: no Math.random()`)
}
async function deleteNewRule() {
let vscodeuri = vscode.Uri.file(
vscode.workspace.workspaceFolders![0].uri.fsPath +
'/rules/no-math-random.yml'
path.join(
vscode.workspace.workspaceFolders![0].uri.fsPath,
'rules',
'no-math-random.yml'
)
)
await vscode.workspace.fs.delete(vscodeuri)
}
async function setRuleDirs(newRuleDirs: string[]) {
let vscodeuri = vscode.Uri.file(
vscode.workspace.workspaceFolders![0].uri.fsPath + '/sgconfig.yml'
path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, 'sgconfig.yml')
)
let content = await vscode.workspace.fs.readFile(vscodeuri)
let configText = new TextDecoder().decode(content)
Expand All @@ -62,7 +69,7 @@ suite('Should update when files change', () => {
try {
assertDiagnosticsEqual(
vscode.languages.getDiagnostics(docUri),
diagnosticss[0]
diagnostics[0]
)
} catch (e) {
console.warn(
Expand All @@ -79,7 +86,7 @@ suite('Should update when files change', () => {
}
assertDiagnosticsEqual(
vscode.languages.getDiagnostics(docUri),
diagnosticss[0]
diagnostics[0]
)
}
})
Expand All @@ -88,15 +95,15 @@ suite('Should update when files change', () => {
await waitForDiagnosticChange(MAX_WAIT_TIME_FOR_UPDATE)
assertDiagnosticsEqual(
vscode.languages.getDiagnostics(docUri),
diagnosticss[1]
diagnostics[1]
)
})
test('Update on rule deletion', async () => {
deleteNewRule()
await waitForDiagnosticChange(MAX_WAIT_TIME_FOR_UPDATE)
assertDiagnosticsEqual(
vscode.languages.getDiagnostics(docUri),
diagnosticss[2]
diagnostics[2]
)
})
test('Update on ruleDirs change to nonexistent path', async () => {
Expand All @@ -109,7 +116,7 @@ suite('Should update when files change', () => {
await waitForDiagnosticChange(MAX_WAIT_TIME_FOR_UPDATE)
assertDiagnosticsEqual(
vscode.languages.getDiagnostics(docUri),
diagnosticss[0]
diagnostics[0]
)
})
})
Expand All @@ -120,7 +127,7 @@ function toRange(sLine: number, sChar: number, eLine: number, eChar: number) {
return new vscode.Range(start, end)
}

function getExpectedDiagnosticss() {
function getExpectedDiagnostics() {
const full = [
{
message: 'No Math.random',
Expand Down
8 changes: 4 additions & 4 deletions src/test/utils.ts → tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as assert from 'assert'
export let doc: vscode.TextDocument
export let editor: vscode.TextEditor

const FIXTURE_PATH = path.resolve(__dirname, '../fixture')

/**
* Prepare the vscode environment for testing by opening the fixture folder
* and activating the extension.
Expand All @@ -15,9 +17,7 @@ export let editor: vscode.TextEditor
export async function activate() {
// The extensionId is `publisher.name` from package.json
const ext = vscode.extensions.getExtension('ast-grep.ast-grep-vscode')!
const fixtureFolderUri = vscode.Uri.file(
path.resolve(__dirname, '../../fixture')
)
const fixtureFolderUri = vscode.Uri.file(FIXTURE_PATH)
await ext.activate()
try {
// open ast-grep project to locate sgconfig.yml
Expand Down Expand Up @@ -118,7 +118,7 @@ async function sleep(ms: number) {
}

export const getDocPath = (p: string) => {
return path.resolve(__dirname, '../../fixture', p)
return path.resolve(FIXTURE_PATH, p)
}
export const getDocUri = (p: string) => {
return vscode.Uri.file(getDocPath(p))
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"target": "es2020",
"lib": ["es2020", "DOM"],
"outDir": "out",
"rootDir": "src",
"rootDir": ".",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"sourceMap": true,
"skipLibCheck": true,
"jsx": "react-jsx"
},
"include": ["src"],
"include": ["src", "tests"],
"exclude": ["node_modules", ".vscode-test"]
}
Loading