From 5740280b02af5d6d73c3f682ed7af38a1127b919 Mon Sep 17 00:00:00 2001 From: bugarela Date: Thu, 5 Sep 2024 16:35:43 -0300 Subject: [PATCH] Remove stringSourceResolver and update tests so they work on windows as well --- quint/src/index.ts | 2 +- quint/src/parsing/sourceResolver.ts | 31 ------------ quint/test/parsing/sourceResolver.test.ts | 61 +++-------------------- quint/test/runtime/compile.test.ts | 6 +-- vscode/quint-vscode/server/test/util.ts | 4 +- 5 files changed, 13 insertions(+), 91 deletions(-) diff --git a/quint/src/index.ts b/quint/src/index.ts index 918b2ed6e..ecd4315e9 100644 --- a/quint/src/index.ts +++ b/quint/src/index.ts @@ -21,7 +21,7 @@ export { findExpressionWithId, findTypeWithId, findDefinitionWithId } from './ir export * from './quintAnalyzer' export * from './quintError' export { newIdGenerator, IdGenerator } from './idGenerator' -export { fileSourceResolver, stringSourceResolver } from './parsing/sourceResolver' +export { fileSourceResolver } from './parsing/sourceResolver' export { format } from './prettierimp' export { prettyQuintEx, prettyTypeScheme, prettyQuintDeclaration } from './graphics' export { Loc } from './ErrorMessage' diff --git a/quint/src/parsing/sourceResolver.ts b/quint/src/parsing/sourceResolver.ts index 518dff166..835d584e1 100644 --- a/quint/src/parsing/sourceResolver.ts +++ b/quint/src/parsing/sourceResolver.ts @@ -111,34 +111,3 @@ export function fileSourceResolver( }, } } - -/** - * Read the source code from a map of strings. This resolver is especially - * useful for tests. - * @param sources a map of paths mapped to text - * @returns a static resolver that uses the map to read the contents. - */ -export const stringSourceResolver = (sources: Map): SourceResolver => { - return { - lookupPath: (stempath: string, importPath: string) => { - return { - normalizedPath: normalize(join(stempath, importPath)), - toSourceName: () => { - return posix.join(stempath, importPath) - }, - } - }, - - load: (lookupPath: SourceLookupPath): Either => { - // We are using nodejs path.join here. - // If we have to decouple this resolver from nodejs in the future, - // we would have to write our own version of join. - const contents = sources.get(lookupPath.normalizedPath) - return contents ? right(contents) : left(`Source not found: '${lookupPath.normalizedPath}'`) - }, - - stempath: (lookupPath: SourceLookupPath): string => { - return dirname(lookupPath.normalizedPath) - }, - } -} diff --git a/quint/test/parsing/sourceResolver.test.ts b/quint/test/parsing/sourceResolver.test.ts index 66655b56a..823da4397 100644 --- a/quint/test/parsing/sourceResolver.test.ts +++ b/quint/test/parsing/sourceResolver.test.ts @@ -3,7 +3,7 @@ import { assert } from 'chai' import { left, right } from '@sweet-monads/either' import { resolve } from 'path' -import { fileSourceResolver, stringSourceResolver } from '../../src/parsing/sourceResolver' +import { fileSourceResolver } from '../../src/parsing/sourceResolver' import { readFileSync } from 'fs' import { lf } from 'eol' @@ -17,14 +17,14 @@ function readQuint(name: string): string { } describe('resolve sources from files', () => { - it('parses empty module', () => { + it('resolves relative path on same dir', () => { const expected = readQuint('./_0001emptyModule') const r = fileSourceResolver() const result = r.load(r.lookupPath(basename, './_0001emptyModule.qnt')) assert.deepEqual(result, right(expected)) }) - it('parses via ..', () => { + it('resolves relative path on parent dir', () => { const expected = readQuint('./_0001emptyModule') const r = fileSourceResolver() const result = r.load(r.lookupPath(basename, '../testFixture/_0001emptyModule.qnt')) @@ -34,61 +34,14 @@ describe('resolve sources from files', () => { it('errors on non-existant', () => { const r = fileSourceResolver() const result = r.load(r.lookupPath(basename, 'does-not-exist')) - assert.deepEqual(result, left(`ENOENT: no such file or directory, open '${basename}/does-not-exist'`)) + const filenameInError = resolve(basename, 'does-not-exist') + assert.deepEqual(result, left(`ENOENT: no such file or directory, open '${filenameInError}'`)) }) it('stemname', () => { const r = fileSourceResolver() const result = r.stempath(r.lookupPath(`${basename}/testFixture`, './_0001emptyModule.qnt')) - assert.deepEqual(result, `${basename}/testFixture`) - }) -}) - -// a static table that contains sources in strings -const staticTable = new Map([ - ['/foo.qnt', 'module foo {}'], - ['/bar.qnt', 'module bar {}'], - ['/baz/baz.qnt', 'module baz {}'], -]) - -describe('resolve sources from strings', () => { - it('parses foo', () => { - const expected = staticTable.get('/foo.qnt') - const r = stringSourceResolver(staticTable) - const result = r.load(r.lookupPath('/', './foo.qnt')) - assert.deepEqual(result, right(expected)) - }) - - it('parses bar via ..', () => { - const expected = staticTable.get('/bar.qnt') - const r = stringSourceResolver(staticTable) - const result = r.load(r.lookupPath('/', './baz/../bar.qnt')) - assert.deepEqual(result, right(expected)) - }) - - it('parses baz via baz/baz.qnt', () => { - const expected = staticTable.get('/baz/baz.qnt') - const r = stringSourceResolver(staticTable) - const result = r.load(r.lookupPath('/', './baz/baz.qnt')) - assert.deepEqual(result, right(expected)) - }) - - it('parses baz via baz.qnt', () => { - const expected = staticTable.get('/baz/baz.qnt') - const r = stringSourceResolver(staticTable) - const result = r.load(r.lookupPath('/baz', './baz.qnt')) - assert.deepEqual(result, right(expected)) - }) - - it('errors on non-existant', () => { - const r = stringSourceResolver(staticTable) - const result = r.load(r.lookupPath('/', 'does-not-exist')) - assert.deepEqual(result, left(`Source not found: '/does-not-exist'`)) - }) - - it('stemname', () => { - const r = stringSourceResolver(staticTable) - const result = r.stempath(r.lookupPath('/baz', './baz.qnt')) - assert.deepEqual(result, '/baz') + const expected = resolve(basename, 'testFixture') + assert.deepEqual(result, expected) }) }) diff --git a/quint/test/runtime/compile.test.ts b/quint/test/runtime/compile.test.ts index 580168b97..b21da5432 100644 --- a/quint/test/runtime/compile.test.ts +++ b/quint/test/runtime/compile.test.ts @@ -18,7 +18,7 @@ import { RuntimeValue } from '../../src/runtime/impl/runtimeValue' import { dedent } from '../textUtils' import { newIdGenerator } from '../../src/idGenerator' import { Rng, newRng } from '../../src/rng' -import { SourceLookupPath, stringSourceResolver } from '../../src/parsing/sourceResolver' +import { SourceLookupPath, fileSourceResolver } from '../../src/parsing/sourceResolver' import { analyzeModules, parse, parseExpressionOrDeclaration, quintErrorToString } from '../../src' import { flattenModules } from '../../src/flattening/fullFlattener' import { newEvaluationState } from '../../src/runtime/impl/base' @@ -34,7 +34,7 @@ const idGen = newIdGenerator() // before the input is evaluated. If not supplied, the context is empty. function assertResultAsString(input: string, expected: string | undefined, evalContext: string = '') { const moduleText = `module contextM { ${evalContext} } module __runtime { import contextM.*\n val ${inputDefName} = ${input} }` - const mockLookupPath = stringSourceResolver(new Map()).lookupPath('/', './mock') + const mockLookupPath = fileSourceResolver(new Map()).lookupPath('/', './mock') const context = compileFromCode( idGen, moduleText, @@ -71,7 +71,7 @@ function assertComputableAsString(computable: Computable, expected: string | und // Compile an input and evaluate a callback in the context function evalInContext(input: string, callable: (ctx: CompilationContext) => Either) { const moduleText = `module __runtime { ${input} }` - const mockLookupPath = stringSourceResolver(new Map()).lookupPath('/', './mock') + const mockLookupPath = fileSourceResolver(new Map()).lookupPath('/', './mock') const context = compileFromCode( idGen, moduleText, diff --git a/vscode/quint-vscode/server/test/util.ts b/vscode/quint-vscode/server/test/util.ts index e5a3ebe15..8e9a33949 100644 --- a/vscode/quint-vscode/server/test/util.ts +++ b/vscode/quint-vscode/server/test/util.ts @@ -20,7 +20,7 @@ import { parsePhase1fromText, parsePhase2sourceResolution, parsePhase3importAndNameResolution, - stringSourceResolver, + fileSourceResolver, } from '@informalsystems/quint' /** @@ -37,7 +37,7 @@ export function parseOrThrow(moduleText: string): [QuintModule[], Map