-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: barebones dependency conversion #61
Changes from all commits
dab7ccc
5da1bfd
c4b2b29
52b257b
d00ace4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { chunk } from 'lodash'; | ||
import fse from 'fs-extra'; | ||
|
||
export const parseGitmodulesDependencies = (): { [key: string]: string } => { | ||
let iniAsStr; | ||
try { | ||
iniAsStr = fse.readFileSync('./.gitmodules', { encoding: 'utf8' }); | ||
} catch { | ||
return {}; | ||
} | ||
const matches = [...iniAsStr.matchAll(/(?:path|url) = (.*)/g)].map(regexMatch => regexMatch[1]); | ||
if (matches.length == 0) return {}; | ||
|
||
// this assumes .gitmodules to be well formed, same amount of paths and urls | ||
const dependencyPairs = chunk(matches, 2); | ||
|
||
// and path is always before url | ||
const dependencyObject = dependencyPairs.reduce<{ [key: string]: string }>((acc, curr) => { | ||
acc[curr[0].replace('lib/', '')] = curr[1]; | ||
return acc; | ||
}, {}); | ||
return dependencyObject; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { expect } from 'chai'; | ||
import mock from 'mock-fs'; | ||
import { parseGitmodulesDependencies } from '../../src/parseGitmodulesDependencies'; | ||
|
||
describe('parseGitmodulesDependencies', () => { | ||
before(() => { | ||
mock.restore(); | ||
}); | ||
after(() => { | ||
mock.restore(); | ||
}); | ||
|
||
describe('with no gitmodules file', function () { | ||
it('should return an empty dependency list', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies).to.deep.eq({}); | ||
}); | ||
}); | ||
|
||
describe('with an empty file', function () { | ||
before(() => { | ||
mock({ | ||
'.gitmodules': '', | ||
}); | ||
}); | ||
it('should return an empty dependency list', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies).to.deep.eq({}); | ||
}); | ||
}); | ||
|
||
describe('with a file with several dependencies', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe worth also having a case where the gitmodules exist but is not in the format we expect it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added them in 52b257b |
||
before(() => { | ||
mock({ | ||
'.gitmodules': ` | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "lib/openzeppelin-contracts"] | ||
path = lib/openzeppelin-contracts | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | ||
`, | ||
}); | ||
}); | ||
it('should be able to parse them', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies['forge-std']).to.eq('https://github.com/foundry-rs/forge-std'); | ||
expect(parsedDependencies['openzeppelin-contracts']).to.eq( | ||
'https://github.com/OpenZeppelin/openzeppelin-contracts', | ||
); | ||
expect(Object.keys(parsedDependencies)).to.have.length(2); | ||
}); | ||
}); | ||
|
||
describe('with a file with extra (valid) keys besides path and url', function () { | ||
before(() => { | ||
mock({ | ||
'.gitmodules': ` | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
branch = . | ||
url = https://github.com/foundry-rs/forge-std | ||
[submodule "lib/openzeppelin-contracts"] | ||
ignore = all | ||
path = lib/openzeppelin-contracts | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | ||
`, | ||
}); | ||
}); | ||
it('should be able to parse them', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies['forge-std']).to.eq('https://github.com/foundry-rs/forge-std'); | ||
expect(parsedDependencies['openzeppelin-contracts']).to.eq( | ||
'https://github.com/OpenZeppelin/openzeppelin-contracts', | ||
); | ||
expect(Object.keys(parsedDependencies)).to.have.length(2); | ||
}); | ||
}); | ||
|
||
describe('with a file where subsection keys are specified in multiple subsection definitions', function () { | ||
before(() => { | ||
// this is valid syntax according to git, but our regex 'parser' can't handle it | ||
mock({ | ||
'.gitmodules': ` | ||
[submodule "lib/openzeppelin-contracts"] | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
[submodule "lib/openzeppelin-contracts"] | ||
path = lib/openzeppelin-contracts | ||
[submodule "lib/forge-std"] | ||
url = https://github.com/foundry-rs/forge-std | ||
`, | ||
}); | ||
}); | ||
it.skip('should be able to parse them', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are some tests being skipped? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to acknowledge, yet thoroughly document the limitations of the current implementation. Example of a previous discussion: #58 (comment) This test in particular was added to document the consequences of not doing proper |
||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies['forge-std']).to.eq('https://github.com/foundry-rs/forge-std'); | ||
expect(parsedDependencies['openzeppelin-contracts']).to.eq( | ||
'https://github.com/OpenZeppelin/openzeppelin-contracts', | ||
); | ||
expect(Object.keys(parsedDependencies)).to.have.length(2); | ||
}); | ||
}); | ||
|
||
describe('with a file urls come before paths', function () { | ||
before(() => { | ||
// this is valid syntax according to git, but our regex 'parser' can't handle it | ||
mock({ | ||
'.gitmodules': ` | ||
[submodule "lib/openzeppelin-contracts"] | ||
url = https://github.com/OpenZeppelin/openzeppelin-contracts | ||
path = lib/openzeppelin-contracts | ||
[submodule "lib/forge-std"] | ||
url = https://github.com/foundry-rs/forge-std | ||
path = lib/forge-std | ||
`, | ||
}); | ||
}); | ||
it.skip('should be able to parse them', function () { | ||
const parsedDependencies = parseGitmodulesDependencies(); | ||
expect(parsedDependencies['forge-std']).to.eq('https://github.com/foundry-rs/forge-std'); | ||
expect(parsedDependencies['openzeppelin-contracts']).to.eq( | ||
'https://github.com/OpenZeppelin/openzeppelin-contracts', | ||
); | ||
expect(Object.keys(parsedDependencies)).to.have.length(2); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If is not, shouldn't it throw an error describing the issue?
I see this as a NTH tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
52b257b
added a test for a valid .gitmodules that breaks it, but am not explicitly throwing an error in that case, as figuring out if the precondition is met or not would involve extra logic