-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manager/gleam): extract locked versions (#31000)
Co-authored-by: Rhys Arkins <[email protected]> Co-authored-by: Michael Kriese <[email protected]>
- Loading branch information
1 parent
6c7316c
commit f619736
Showing
5 changed files
with
337 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { codeBlock } from 'common-tags'; | ||
import { mocked } from '../../../../test/util'; | ||
import { logger } from '../../../logger'; | ||
import * as _fs from '../../../util/fs'; | ||
import { extractLockFileVersions, parseLockFile } from './locked-version'; | ||
|
||
jest.mock('../../../util/fs'); | ||
|
||
const fs = mocked(_fs); | ||
|
||
const lockFileContent = codeBlock` | ||
packages = [ | ||
{ name = "foo", version = "1.0.4", build_tools = ["gleam"], requirements = ["bar"], otp_app = "foo", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" }, | ||
{ name = "bar", version = "2.1.0", build_tools = ["rebar3"], requirements = [], otp_app = "bar", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" }, | ||
] | ||
[requirements] | ||
foo = { version = ">= 1.0.0 and < 2.0.0" } | ||
`; | ||
|
||
describe('modules/manager/gleam/locked-version', () => { | ||
describe('extractLockFileVersions()', () => { | ||
it('returns null for missing lock file', async () => { | ||
expect(await extractLockFileVersions('manifest.toml')).toBeNull(); | ||
}); | ||
|
||
it('returns null for invalid lock file', async () => { | ||
fs.readLocalFile.mockResolvedValueOnce('foo'); | ||
expect(await extractLockFileVersions('manifest.toml')).toBeNull(); | ||
}); | ||
|
||
it('returns empty map for lock file without packages', async () => { | ||
fs.readLocalFile.mockResolvedValueOnce('[requirements]'); | ||
expect(await extractLockFileVersions('manifest.toml')).toEqual(new Map()); | ||
}); | ||
|
||
it('returns a map of package versions', async () => { | ||
fs.readLocalFile.mockResolvedValueOnce(lockFileContent); | ||
expect(await extractLockFileVersions('manifest.toml')).toEqual( | ||
new Map([ | ||
['foo', ['1.0.4']], | ||
['bar', ['2.1.0']], | ||
]), | ||
); | ||
}); | ||
}); | ||
|
||
describe('parseLockFile', () => { | ||
it('parses lockfile string into an object', () => { | ||
const parseLockFileResult = parseLockFile(lockFileContent); | ||
logger.debug({ parseLockFileResult }, 'parseLockFile'); | ||
expect(parseLockFileResult).toStrictEqual({ | ||
packages: [ | ||
{ | ||
name: 'foo', | ||
version: '1.0.4', | ||
requirements: ['bar'], | ||
}, | ||
{ | ||
name: 'bar', | ||
version: '2.1.0', | ||
requirements: [], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('can deal with invalid lockfiles', () => { | ||
const lockFile = 'foo'; | ||
const parseLockFileResult = parseLockFile(lockFile); | ||
expect(parseLockFileResult).toBeNull(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { logger } from '../../../logger'; | ||
import { coerceArray } from '../../../util/array'; | ||
import { readLocalFile } from '../../../util/fs'; | ||
import { ManifestToml } from './schema'; | ||
|
||
export async function extractLockFileVersions( | ||
lockFilePath: string, | ||
): Promise<Map<string, string[]> | null> { | ||
const content = await readLocalFile(lockFilePath, 'utf8'); | ||
if (!content) { | ||
logger.debug(`Gleam lock file ${lockFilePath} not found`); | ||
return null; | ||
} | ||
|
||
const versionsByPackage = new Map<string, string[]>(); | ||
const lock = parseLockFile(content); | ||
if (!lock) { | ||
logger.debug(`Error parsing Gleam lock file ${lockFilePath}`); | ||
return null; | ||
} | ||
for (const pkg of coerceArray(lock.packages)) { | ||
const versions = coerceArray(versionsByPackage.get(pkg.name)); | ||
versions.push(pkg.version); | ||
versionsByPackage.set(pkg.name, versions); | ||
} | ||
return versionsByPackage; | ||
} | ||
|
||
export function parseLockFile(lockFileContent: string): ManifestToml | null { | ||
const res = ManifestToml.safeParse(lockFileContent); | ||
if (res.success) { | ||
return res.data; | ||
} | ||
logger.debug({ err: res.error }, 'Error parsing manifest.toml.'); | ||
return null; | ||
} |
Oops, something went wrong.