Skip to content

Commit

Permalink
Remove stringSourceResolver and update tests so they work on windows …
Browse files Browse the repository at this point in the history
…as well
  • Loading branch information
bugarela committed Sep 5, 2024
1 parent 0813ab6 commit 5740280
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 91 deletions.
2 changes: 1 addition & 1 deletion quint/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
31 changes: 0 additions & 31 deletions quint/src/parsing/sourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>): SourceResolver => {
return {
lookupPath: (stempath: string, importPath: string) => {
return {
normalizedPath: normalize(join(stempath, importPath)),
toSourceName: () => {
return posix.join(stempath, importPath)
},
}
},

load: (lookupPath: SourceLookupPath): Either<string, string> => {
// 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)
},
}
}
61 changes: 7 additions & 54 deletions quint/test/parsing/sourceResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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'))
Expand All @@ -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<string, string>([
['/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)
})
})
6 changes: 3 additions & 3 deletions quint/test/runtime/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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,
Expand Down Expand Up @@ -71,7 +71,7 @@ function assertComputableAsString(computable: Computable, expected: string | und
// Compile an input and evaluate a callback in the context
function evalInContext<T>(input: string, callable: (ctx: CompilationContext) => Either<string, T>) {
const moduleText = `module __runtime { ${input} }`
const mockLookupPath = stringSourceResolver(new Map()).lookupPath('/', './mock')
const mockLookupPath = fileSourceResolver(new Map()).lookupPath('/', './mock')
const context = compileFromCode(
idGen,
moduleText,
Expand Down
4 changes: 2 additions & 2 deletions vscode/quint-vscode/server/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
parsePhase1fromText,
parsePhase2sourceResolution,
parsePhase3importAndNameResolution,
stringSourceResolver,
fileSourceResolver,

Check failure on line 23 in vscode/quint-vscode/server/test/util.ts

View workflow job for this annotation

GitHub Actions / quint-vscode-linting

Member 'fileSourceResolver' of the import declaration should be sorted alphabetically
} from '@informalsystems/quint'

/**
Expand All @@ -37,7 +37,7 @@ export function parseOrThrow(moduleText: string): [QuintModule[], Map<bigint, Lo
const idgen = newIdGenerator()
const phase1Data = parsePhase1fromText(idgen, moduleText, 'mocked_path')

const resolver = stringSourceResolver(new Map())
const resolver = fileSourceResolver(new Map())
const mainPath = resolver.lookupPath('mocked_path', './main')
const phase2Data = parsePhase2sourceResolution(idgen, resolver, mainPath, phase1Data)

Expand Down

0 comments on commit 5740280

Please sign in to comment.