Update tests to use node fs (#7446)

Also don't re-export fs and path through utils
This commit is contained in:
Matt Godbolt
2025-02-25 13:39:26 -06:00
committed by GitHub
parent 698f9944cd
commit 9a8673f950
20 changed files with 73 additions and 52 deletions

View File

@@ -22,6 +22,8 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import {beforeAll, describe, expect, it} from 'vitest';
import {CompilationEnvironment} from '../lib/compilation-env.js';
@@ -30,7 +32,7 @@ import * as utils from '../lib/utils.js';
import {ParsedAsmResultLine} from '../types/asmresult/asmresult.interfaces.js';
import {CompilerInfo} from '../types/compiler.interfaces.js';
import {fs, makeCompilationEnvironment} from './utils.js';
import {makeCompilationEnvironment} from './utils.js';
const languages = {
androidJava: {id: 'android-java'},

View File

@@ -22,6 +22,9 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs/promises';
import path from 'node:path';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
import {BaseCompiler} from '../lib/base-compiler.js';
@@ -37,8 +40,6 @@ import {CompilerOverrideType, ConfiguredOverrides} from '../types/compilation/co
import {CompilerInfo} from '../types/compiler.interfaces.js';
import {
fs,
path,
makeCompilationEnvironment,
makeFakeCompilerInfo,
makeFakeParseFiltersAndOutputOptions,
@@ -86,6 +87,7 @@ describe('Basic compiler invariants', () => {
function testIncludeG(text: string) {
expect(compiler.checkSource(text)).toBeNull();
}
testIncludeG('#include <iostream>');
testIncludeG('#include <iostream> // <..>');
testIncludeG('#include <type_traits> // for std::is_same_v<...>');
@@ -96,6 +98,7 @@ describe('Basic compiler invariants', () => {
function testIncludeNotG(text: string) {
expect(compiler.checkSource(text)).toEqual('<stdin>:1:1: no absolute or relative includes please');
}
testIncludeNotG('#include <./.bashrc>');
testIncludeNotG('#include </dev/null> // <..>');
testIncludeNotG('#include <../fish.config> // for std::is_same_v<...>');

View File

@@ -22,6 +22,8 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import path from 'node:path';
import {Readable} from 'node:stream';
import {GetObjectCommand, NoSuchKey, PutObjectCommand, S3} from '@aws-sdk/client-s3';
@@ -37,7 +39,7 @@ import {NullCache} from '../lib/cache/null.js';
import {OnDiskCache} from '../lib/cache/on-disk.js';
import {S3Cache} from '../lib/cache/s3.js';
import {fs, path, newTempDir} from './utils.js';
import {newTempDir} from './utils.js';
function basicTests(factory: () => BaseCache) {
it('should start empty', async () => {

View File

@@ -22,14 +22,18 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import * as fsSync from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import {describe, expect, it} from 'vitest';
import * as cfg from '../lib/cfg/cfg.js';
import {fs, path, makeFakeCompilerInfo, resolvePathFromTestRoot} from './utils.js';
import {makeFakeCompilerInfo, resolvePathFromTestRoot} from './utils.js';
async function DoCfgTest(cfgArg, filename, isLlvmIr = false) {
const contents = await fs.readJson(filename, 'utf8');
const contents = JSON.parse(await fs.readFile(filename, 'utf8'));
const structure = cfg.generateStructure(
makeFakeCompilerInfo({
compilerType: '',
@@ -47,7 +51,7 @@ describe('Cfg test cases', () => {
// For backwards compatability reasons, we have a sync readdir here. For details, see
// the git blame of this file.
// TODO: Consider replacing with https://github.com/vitest-dev/vitest/issues/703
const files = fs.readdirSync(testcasespath);
const files = fsSync.readdirSync(testcasespath);
describe('gcc', () => {
for (const filename of files.filter(x => x.includes('gcc'))) {

View File

@@ -22,6 +22,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import _ from 'underscore';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
@@ -29,8 +30,6 @@ import {unwrap} from '../lib/assert.js';
import {languages} from '../lib/languages.js';
import * as properties from '../lib/properties.js';
import {fs} from './utils.js';
describe('Live site checks', () => {
let ceProps;
let compilerProps;

View File

@@ -22,6 +22,10 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import * as fsSync from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import {describe, expect, it} from 'vitest';
import {unwrap} from '../lib/assert.js';
@@ -35,7 +39,7 @@ import * as properties from '../lib/properties.js';
import {SymbolStore} from '../lib/symbol-store.js';
import * as utils from '../lib/utils.js';
import {fs, path, makeFakeCompilerInfo, resolvePathFromTestRoot} from './utils.js';
import {makeFakeCompilerInfo, resolvePathFromTestRoot} from './utils.js';
const cppfiltpath = 'c++filt';
@@ -305,8 +309,7 @@ describe('Basic demangling', () => {
});
async function readResultFile(filename: string) {
const data = await fs.readFile(filename);
const asm = utils.splitLines(data.toString()).map(line => {
const asm = utils.splitLines(await fs.readFile(filename, 'utf-8')).map(line => {
return {text: line};
});
@@ -329,7 +332,7 @@ if (process.platform === 'linux') {
// For backwards compatability reasons, we have a sync readdir here. For details, see
// the git blame of this file.
// TODO: Consider replacing with https://github.com/vitest-dev/vitest/issues/703
const files = fs.readdirSync(testcasespath);
const files = fsSync.readdirSync(testcasespath);
for (const filename of files) {
if (filename.endsWith('.asm')) {

View File

@@ -22,6 +22,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import path from 'node:path';
import {describe, expect, it} from 'vitest';
@@ -34,7 +35,7 @@ import {AsmParser} from '../lib/parsers/asm-parser.js';
import {fakeProps} from '../lib/properties.js';
import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js';
import {fs, resolvePathFromTestRoot} from './utils.js';
import {resolvePathFromTestRoot} from './utils.js';
function processAsm(filename: string, filters: ParseFiltersAndOutputOptions) {
const file = fs.readFileSync(filename, 'utf8');

View File

@@ -22,13 +22,15 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import {beforeAll, describe, expect, it} from 'vitest';
import {GolangCompiler} from '../lib/compilers/golang.js';
import * as utils from '../lib/utils.js';
import {LanguageKey} from '../types/languages.interfaces.js';
import {fs, makeCompilationEnvironment, makeFakeCompilerInfo} from './utils.js';
import {makeCompilationEnvironment, makeFakeCompilerInfo} from './utils.js';
const languages = {
go: {id: 'go' as LanguageKey},

View File

@@ -22,6 +22,8 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import {beforeAll, describe, expect, it} from 'vitest';
import {CompilationEnvironment} from '../lib/compilation-env.js';
@@ -30,7 +32,7 @@ import * as utils from '../lib/utils.js';
import {ParsedAsmResultLine} from '../types/asmresult/asmresult.interfaces.js';
import {CompilerInfo} from '../types/compiler.interfaces.js';
import {fs, makeCompilationEnvironment} from './utils.js';
import {makeCompilationEnvironment} from './utils.js';
const languages = {
java: {id: 'java'},

View File

@@ -22,12 +22,13 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import path from 'node:path';
import {describe, expect, it} from 'vitest';
import {languages} from '../lib/languages.js';
import {fs, path} from './utils.js';
describe('Language definitions tests', () => {
it('Has id equal to object key', () => {
for (const languageKey of Object.keys(languages)) expect(languages[languageKey].id).toEqual(languageKey);

View File

@@ -19,7 +19,7 @@
import path from 'node:path';
import fs from 'fs-extra';
import fs from 'node:fs/promises';
import {beforeAll, describe, expect, it} from 'vitest';
import {BaseCompiler} from '../lib/base-compiler.js';

View File

@@ -22,6 +22,8 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import cloneDeep from 'lodash.clonedeep';
import {beforeAll, describe, expect, it} from 'vitest';
@@ -29,8 +31,6 @@ import {LlvmAstParser} from '../lib/llvm-ast.js';
import * as properties from '../lib/properties.js';
import * as utils from '../lib/utils.js';
import {fs} from './utils.js';
const languages = {
'c++': {id: 'c++'},
};

View File

@@ -22,13 +22,15 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import {beforeAll, describe, expect, it} from 'vitest';
import {OdinCompiler} from '../lib/compilers/odin.js';
import {CompilerOutputOptions} from '../types/features/filters.interfaces.js';
import {LanguageKey} from '../types/languages.interfaces.js';
import {fs, makeCompilationEnvironment, makeFakeCompilerInfo} from './utils.js';
import {makeCompilationEnvironment, makeFakeCompilerInfo} from './utils.js';
const languages = {
odin: {id: 'odin' as LanguageKey},

View File

@@ -22,13 +22,16 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs/promises';
import path from 'node:path';
import {describe, expect, it} from 'vitest';
import {Packager} from '../lib/packager.js';
import {fs, path, newTempDir} from './utils.js';
import {newTempDir} from './utils.js';
function writeTestFile(filepath) {
async function writeTestFile(filepath) {
return fs.writeFile(filepath, '#!/bin/sh\n\necho Hello, world!\n\n');
}
@@ -44,7 +47,7 @@ describe('Packager', () => {
const targzPath = path.join(dirPath, 'package.tgz');
await pack.package(dirPath, targzPath);
await expect(fs.exists(targzPath)).resolves.toBe(true);
await expect(fs.stat(targzPath)).resolves.toBeDefined();
},
{timeout: 5000},
);
@@ -65,7 +68,7 @@ describe('Packager', () => {
await pack2.unpack(targzPath, unpackPath);
const unpackedFilepath = path.join(unpackPath, 'hello.txt');
await expect(fs.exists(unpackedFilepath)).resolves.toBe(true);
await expect(fs.stat(unpackedFilepath)).resolves.toBeDefined;
},
{timeout: 5000},
);

View File

@@ -22,6 +22,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs/promises';
import path from 'node:path';
import {beforeAll, describe, expect, it} from 'vitest';
@@ -32,7 +33,7 @@ import {FPCCompiler} from '../lib/compilers/pascal.js';
import {PascalDemangler} from '../lib/demangler/index.js';
import * as utils from '../lib/utils.js';
import {fs, makeCompilationEnvironment} from './utils.js';
import {makeCompilationEnvironment} from './utils.js';
const languages = {
pascal: {id: 'pascal'},
@@ -360,7 +361,7 @@ describe('Pascal', () => {
});
it('Should have line numbering', async () => {
const asmLines = utils.splitLines((await fs.readFile('test/pascal/asm-example.s')).toString());
const asmLines = utils.splitLines(await fs.readFile('test/pascal/asm-example.s', 'utf-8'));
compiler.preProcessLines(asmLines);
expect(asmLines).toContain('# [output.pas]');
expect(asmLines).toContain(' .file 1 "output.pas"');
@@ -405,10 +406,10 @@ describe('Pascal', () => {
});
});
describe('Pascal filetype detection', () => {
describe('Pascal filetype detection', async () => {
const pasUtils = new PascalUtils();
const progSource = fs.readFileSync('test/pascal/prog.dpr').toString('utf8');
const unitSource = fs.readFileSync('test/pascal/example.pas').toString('utf8');
const progSource = await fs.readFile('test/pascal/prog.dpr', 'utf-8');
const unitSource = await fs.readFile('test/pascal/example.pas', 'utf-8');
it('Should detect simple program', () => {
expect(pasUtils.isProgram(progSource)).toEqual(true);
@@ -439,7 +440,7 @@ describe('Pascal', () => {
const dirPath = await compiler.newTempDir();
const filters = {};
const files = [];
const source = fs.readFileSync('examples/pascal/default.pas').toString('utf8');
const source = await fs.readFile('examples/pascal/default.pas', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -452,7 +453,7 @@ describe('Pascal', () => {
const dirPath = await compiler.newTempDir();
const filters = {};
const files = [];
const source = fs.readFileSync('test/pascal/example.pas').toString('utf8');
const source = await fs.readFile('test/pascal/example.pas', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -465,7 +466,7 @@ describe('Pascal', () => {
const dirPath = await compiler.newTempDir();
const filters = {};
const files = [];
const source = fs.readFileSync('test/pascal/prog.dpr').toString('utf8');
const source = await fs.readFile('test/pascal/prog.dpr', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -483,7 +484,7 @@ describe('Pascal', () => {
contents: '{ hello\n world }',
},
];
const source = fs.readFileSync('test/pascal/prog.dpr').toString('utf8');
const source = await fs.readFile('test/pascal/prog.dpr', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -511,7 +512,7 @@ describe('Pascal', () => {
const dirPath = await compiler.newTempDir();
const filters = {};
const files = [];
const source = fs.readFileSync('examples/pascal/default.pas').toString('utf8');
const source = await fs.readFile('examples/pascal/default.pas', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -524,7 +525,7 @@ describe('Pascal', () => {
const dirPath = await compiler.newTempDir();
const filters = {};
const files = [];
const source = fs.readFileSync('test/pascal/example.pas').toString('utf8');
const source = await fs.readFile('test/pascal/example.pas', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -537,7 +538,7 @@ describe('Pascal', () => {
const dirPath = await compiler.newTempDir();
const filters = {};
const files = [];
const source = fs.readFileSync('test/pascal/prog.dpr').toString('utf8');
const source = await fs.readFile('test/pascal/prog.dpr', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);
@@ -555,7 +556,7 @@ describe('Pascal', () => {
contents: '{ hello\n world }',
},
];
const source = fs.readFileSync('test/pascal/prog.dpr').toString('utf8');
const source = await fs.readFile('test/pascal/prog.dpr', 'utf-8');
const writeSummary = await compiler.writeAllFiles(dirPath, source, files, filters);

View File

@@ -22,13 +22,13 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs';
import {describe, expect, it} from 'vitest';
import {ClientStateGoldenifier, ClientStateNormalizer} from '../lib/clientstate-normalizer.js';
import {ClientState} from '../lib/clientstate.js';
import {fs} from './utils.js';
describe('Normalizing clientstate', () => {
it('Should translate 2 compilers GL layout to clientstate', () => {
const normalizer = new ClientStateNormalizer();

View File

@@ -22,6 +22,7 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import path from 'node:path';
import {describe, expect, it} from 'vitest';
import {
@@ -35,8 +36,6 @@ import {ToolEnv} from '../lib/tooling/base-tool.interface.js';
import {CompilerDropinTool} from '../lib/tooling/compiler-dropin-tool.js';
import {ToolInfo} from '../types/tool.interfaces.js';
import {path} from './utils.js';
describe('CompilerDropInTool', () => {
it('Should support llvm based compilers', () => {
const tool = new CompilerDropinTool({} as ToolInfo, {} as ToolEnv);

View File

@@ -22,13 +22,13 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs/promises';
import express from 'express';
import {beforeAll, describe, expect, it} from 'vitest';
import {RouteAPI} from '../lib/handlers/route-api.js';
import {fs} from './utils.js';
describe('Basic unfurls', () => {
const router = null as any as express.Router;
let config;
@@ -44,7 +44,7 @@ describe('Basic unfurls', () => {
},
storageHandler: {
expandId: async id => {
const json = await fs.readFile('test/state/' + id + '.json');
const json = await fs.readFile('test/state/' + id + '.json', 'utf-8');
return {
config: json,
};

View File

@@ -22,7 +22,9 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import fs from 'node:fs/promises';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {describe, expect, it} from 'vitest';
@@ -31,8 +33,6 @@ import winston from 'winston';
import {makeLogStream} from '../lib/logger.js';
import * as utils from '../lib/utils.js';
import {fs} from './utils.js';
describe('Splits lines', () => {
it('handles empty input', () => {
expect(utils.splitLines('')).toEqual([]);
@@ -550,7 +550,7 @@ describe('Hash interface', () => {
describe('GoldenLayout utils', () => {
it('finds every editor & compiler', async () => {
const state = await fs.readJson('test/example-states/default-state.json');
const state = JSON.parse(await fs.readFile('test/example-states/default-state.json', 'utf-8'));
const contents = utils.glGetMainContents(state.content);
expect(contents).toEqual({
editors: [

View File

@@ -26,7 +26,6 @@ import os from 'node:os';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import fs from 'fs-extra';
import temp from 'temp';
import {expect} from 'vitest';
@@ -88,5 +87,3 @@ export function newTempDir() {
temp.track(true);
return temp.mkdirSync({prefix: 'compiler-explorer-tests', dir: os.tmpdir()});
}
export {path, fs};