Skip to content

Commit

Permalink
update blocked ESM only packages (execa, process-exists)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed May 11, 2023
1 parent 178948f commit d44731f
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 115 deletions.
5 changes: 3 additions & 2 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1628,14 +1628,15 @@
"test-vscode": "node ./dist/test/runTest.js",
"test-e2e": "wdio run ./src/test/e2e/wdio.conf.ts",
"test": "jest --collect-coverage",
"setup-venv": "node ./scripts/virtualenv-install.js",
"cover-vscode-run": "node ./scripts/coverIntegrationTests.js",
"vscode:prepublish": ""
},
"dependencies": {
"@hediet/std": "0.6.0",
"@vscode/extension-telemetry": "0.7.7",
"appdirs": "1.1.0",
"execa": "5.1.1",
"execa": "7.1.1",
"fs-extra": "11.1.1",
"js-yaml": "4.1.0",
"json5": "2.2.3",
Expand All @@ -1646,6 +1647,7 @@
"lodash.merge": "4.6.2",
"lodash.omit": "4.5.0",
"node-fetch": "2.6.9",
"process-exists": "5.0.0",
"tree-kill": "1.2.2",
"uuid": "9.0.0",
"vega-util": "1.17.2",
Expand Down Expand Up @@ -1690,7 +1692,6 @@
"lint-staged": "13.2.2",
"mocha": "10.2.0",
"mock-require": "3.0.3",
"process-exists": "4.1.0",
"shx": "0.3.4",
"sinon": "15.0.4",
"sinon-chai": "3.7.0",
Expand Down
48 changes: 27 additions & 21 deletions extension/scripts/coverIntegrationTests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { resolve, join } = require('path')
const { writeFileSync } = require('fs-extra')
const execa = require('execa')

const getExeca = async () => {
const { execa } = await import('execa')
return execa
}

let activationEvents = []
let failed
Expand All @@ -18,26 +22,28 @@ activationEvents = packageJson.activationEvents
packageJson.activationEvents = ['onStartupFinished']
writeFileSync(packageJsonPath, JSON.stringify(packageJson))

const tests = execa('node', [join(cwd, 'dist', 'test', 'runTest.js')], {
cwd
})

pipe(tests)
tests
.then(() => {})
.catch(() => {
failed = true
getExeca().then(execa => {
const tests = execa('node', [join(cwd, 'dist', 'test', 'runTest.js')], {
cwd
})
.finally(() => {
packageJson.activationEvents = activationEvents

writeFileSync(packageJsonPath, JSON.stringify(packageJson))

const prettier = execa('prettier', ['--write', 'package.json'], { cwd })
pipe(prettier)
prettier.then(() => {
if (failed) {
process.exit(1)
}
pipe(tests)
tests
.then(() => {})
.catch(() => {
failed = true
})
})
.finally(() => {
packageJson.activationEvents = activationEvents

writeFileSync(packageJsonPath, JSON.stringify(packageJson))

const prettier = execa('prettier', ['--write', 'package.json'], { cwd })
pipe(prettier)
prettier.then(() => {
if (failed) {
process.exit(1)
}
})
})
})
13 changes: 13 additions & 0 deletions extension/scripts/virtualenv-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { join, resolve } = require('path')
require('../dist/vscode/mockModule')

const importModuleAfterMockingVsCode = async () => {
const { setupTestVenv } = require('../dist/python')
return setupTestVenv
}

importModuleAfterMockingVsCode().then(setupTestVenv => {
const cwd = resolve(__dirname, '..', '..', 'demo')

setupTestVenv(cwd, '.env', '-r', join('.', 'requirements.txt'))
})
8 changes: 5 additions & 3 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { GitExecutor } from './cli/git/executor'
import { GitReader } from './cli/git/reader'
import { Setup } from './setup'
import { definedAndNonEmpty } from './util/array'
import { stopProcesses } from './process/execution'
import { esmModulesImported, stopProcesses } from './process/execution'
import { Flag } from './cli/dvc/constants'
import { LanguageClient } from './languageClient'
import { collectRunningExperimentPids } from './experiments/processExecution/collect'
Expand Down Expand Up @@ -304,8 +304,10 @@ class Extension extends Disposable {
let extension: undefined | Extension

export function activate(context: ExtensionContext): void {
extension = new Extension(context)
context.subscriptions.push(extension)
void esmModulesImported.then(() => {
extension = new Extension(context)
context.subscriptions.push(extension)
})
}

export function deactivate(): void {
Expand Down
32 changes: 0 additions & 32 deletions extension/src/process/execution.test.ts

This file was deleted.

31 changes: 28 additions & 3 deletions extension/src/process/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@ import { ChildProcess } from 'child_process'
import { Readable } from 'stream'
import { Event, EventEmitter } from 'vscode'
import { Disposable } from '@hediet/std/disposable'
import execa from 'execa'
import doesProcessExists from 'process-exists'
import { Deferred } from '@hediet/std/synchronization'
import kill from 'tree-kill'
import { getProcessPlatform } from '../env'

const deferred = new Deferred()
export const esmModulesImported = deferred.promise

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type EsmExeca = typeof import('execa').execa
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
type EsmProcessExists = typeof import('process-exists').processExists

const envCanImportEsm = process.env.NODE_ENV !== 'test'

let execa: EsmExeca
let doesProcessExist: EsmProcessExists
const importEsmModules = async () => {
const [{ execa: esmExeca }, { processExists: esmProcessExists }] =
await Promise.all([import('execa'), import('process-exists')])
execa = esmExeca
doesProcessExist = esmProcessExists
deferred.resolve()
}

if (envCanImportEsm) {
void importEsmModules()
}

interface RunningProcess extends ChildProcess {
all?: Readable
}
Expand Down Expand Up @@ -86,7 +111,7 @@ export const executeProcess = async (
}

export const processExists = (pid: number): Promise<boolean> =>
doesProcessExists(pid)
doesProcessExist(pid)

export const stopProcesses = async (pids: number[]): Promise<boolean> => {
let allKilled = true
Expand Down
8 changes: 4 additions & 4 deletions extension/src/python/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'path'
import { setupVenv } from '.'
import { setupTestVenv } from '.'
import { Process, createProcess } from '../process/execution'
import { getProcessPlatform } from '../env'

Expand All @@ -16,15 +16,15 @@ beforeEach(() => {
jest.resetAllMocks()
})

describe('setupVenv', () => {
describe('setupTestVenv', () => {
it('should create the correct python processes on sane operating systems', async () => {
mockedCreateProcess.mockResolvedValue(mockedProcess)
mockedGetProcessPlatform.mockReturnValue('freebsd')

const envDir = '.env'
const cwd = __dirname

await setupVenv(__dirname, envDir, 'dvc')
await setupTestVenv(__dirname, envDir, 'dvc')

expect(mockedCreateProcess).toHaveBeenCalledTimes(3)
expect(mockedCreateProcess).toHaveBeenCalledWith({
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('setupVenv', () => {
const envDir = '.env'
const cwd = __dirname

await setupVenv(__dirname, envDir, '-r', 'requirements.txt')
await setupTestVenv(__dirname, envDir, '-r', 'requirements.txt')

expect(mockedCreateProcess).toHaveBeenCalledTimes(3)
expect(mockedCreateProcess).toHaveBeenCalledWith({
Expand Down
10 changes: 8 additions & 2 deletions extension/src/python/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { getVenvBinPath } from './path'
import { getProcessPlatform } from '../env'
import { exists } from '../fileSystem'
import { Logger } from '../common/logger'
import { createProcess, executeProcess, Process } from '../process/execution'
import {
createProcess,
esmModulesImported,
executeProcess,
Process
} from '../process/execution'

const sendOutput = (process: Process) => {
process.all?.on('data', chunk =>
Expand All @@ -29,11 +34,12 @@ export const installPackages = (
export const getDefaultPython = (): string =>
getProcessPlatform() === 'win32' ? 'python' : 'python3'

export const setupVenv = async (
export const setupTestVenv = async (
cwd: string,
envDir: string,
...installArgs: string[]
) => {
await esmModulesImported
if (!exists(join(cwd, envDir))) {
const initVenv = createProcess({
args: ['-m', 'venv', envDir],
Expand Down
8 changes: 4 additions & 4 deletions extension/src/test/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ require('../../vscode/mockModule')
const importModulesAfterMockingVsCode = () => {
const { removeDir } = require('../../fileSystem')
const { runMocha } = require('../util/mocha')
const { setupVenv } = require('../../python')
const { setupTestVenv } = require('../../python')

return { removeDir, runMocha, setupVenv }
return { removeDir, runMocha, setupTestVenv }
}

const { setupVenv, removeDir, runMocha } = importModulesAfterMockingVsCode()
const { setupTestVenv, removeDir, runMocha } = importModulesAfterMockingVsCode()

async function main() {
try {
Expand All @@ -19,7 +19,7 @@ async function main() {
'ts',
async () => {
await mkdirp(TEMP_DIR)
await setupVenv(
await setupTestVenv(
TEMP_DIR,
ENV_DIR,
'git+https://github.com/iterative/dvc#egg=dvc[s3]'
Expand Down
2 changes: 1 addition & 1 deletion extension/src/test/e2e/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Key } from 'webdriverio'
import { $$, browser } from '@wdio/globals'
import { ViewControl } from 'wdio-vscode-service'
import { PlotsWebview } from './pageObjects/plotsWebview'
import { PlotsWebview } from './pageObjects/plotsWebview.js'

const findProgressBars = () => $$('.monaco-progress-container')

Expand Down
6 changes: 4 additions & 2 deletions extension/src/test/suite/cli/child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ require('../../../vscode/mockModule')

const importModuleAfterMockingVsCode = () => {
const { Cli } = require('../../../cli')
return { Cli }
const { esmModulesImported } = require('../../../process/execution')
return { Cli, esmModulesImported }
}

const main = async () => {
const { Cli } = importModuleAfterMockingVsCode()
const { Cli, esmModulesImported } = importModuleAfterMockingVsCode()

await esmModulesImported
const cli = new Cli()

const options = getOptions('background')
Expand Down
36 changes: 36 additions & 0 deletions extension/src/test/suite/process/execution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import process from 'process'
import { describe, it, suite } from 'mocha'
import { expect } from 'chai'
import { executeProcess, processExists } from '../../../process/execution'

suite('Process Manager Test Suite', () => {
describe('executeProcess', () => {
it('should be able to run a process', async () => {
const output = await executeProcess({
args: ['some', 'text'],
cwd: __dirname,
executable: 'echo'
})
expect(output).to.match(/some.*text/)
})

it('should return the stderr if the process throws with stderr', async () => {
await expect(
executeProcess({
args: ['me', 'outside'],
cwd: __dirname,
executable: 'find'
})
).to.be.eventually.rejected
})
})

describe('processExists', () => {
it('should return true if the process exists', async () => {
expect(await processExists(process.pid)).to.be.true
})
it('should return false if it does not', async () => {
expect(await processExists(-123.321)).to.be.false
})
})
})
1 change: 1 addition & 0 deletions extension/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"moduleResolution": "node16",
"module": "commonjs",
"target": "es6",
"outDir": "dist",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"postinstall": "husky install && git submodule init && git submodule update",
"storybook": "yarn workspace dvc-vscode-webview storybook",
"build-storybook": "yarn turbo run build-storybook --filter=dvc-vscode-webview",
"setup:venv": "ts-node ./scripts/virtualenv-install.ts",
"setup:venv": "yarn turbo run lint:build && yarn workspace dvc run setup-venv",
"scheduled:cli:test": "ts-node ./extension/src/test/cli/index.ts",
"create-svgs": "ts-node ./scripts/create-svgs.ts",
"svgr": "yarn workspace dvc-vscode-webview svgr"
Expand Down
2 changes: 1 addition & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ignoreDeps": ["@types/node", "@types/vscode", "execa", "process-exists"],
"ignoreDeps": ["@types/node", "@types/vscode"],
"extends": ["config:base"],
"packageRules": [
{
Expand Down
14 changes: 0 additions & 14 deletions scripts/virtualenv-install.ts

This file was deleted.

Loading

0 comments on commit d44731f

Please sign in to comment.