-
Notifications
You must be signed in to change notification settings - Fork 86
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: add protobuf #717
feat: add protobuf #717
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,5 +90,43 @@ describe('test/common/adapter/binary/GithubBinary.test.ts', () => { | |
assert(matchFile2); | ||
assert(matchFile3); | ||
}); | ||
|
||
it('should fetch protobuf', async () => { | ||
const response = await TestUtil.readJSONFile(TestUtil.getFixtures('protobuf-releases.json')); | ||
app.mockHttpclient(/https:\/\/api\.github\.com\/repos\/protocolbuffers\/protobuf\/releases/, 'GET', { | ||
data: response, | ||
status: 200, | ||
}); | ||
let result = await binary.fetch('/', 'protobuf'); | ||
assert(result); | ||
assert(result.items.length > 0); | ||
let matchDir = false; | ||
for (const item of result.items) { | ||
assert(item.isDir === true); | ||
if (item.name === 'v28.2/') { | ||
matchDir = true; | ||
} | ||
} | ||
assert(matchDir); | ||
|
||
result = await binary.fetch('/v28.2/', 'protobuf'); | ||
assert(result?.items.every(item => !/{.*}/.test(item.url))); | ||
|
||
result = await binary.fetch('/v28.2/', 'protobuf'); | ||
assert(result); | ||
assert(result.items.length > 0); | ||
console.log(JSON.stringify(result.items, null, 2)); | ||
let matchFile1 = false; | ||
for (const item of result.items) { | ||
assert(item.isDir === false); | ||
if (item.name === 'protoc-28.2-linux-aarch_64.zip') { | ||
assert(item.date === '2024-09-18T21:02:40Z'); | ||
assert(item.size === 3218760); | ||
assert.equal(item.url, 'https://github.com/protocolbuffers/protobuf/releases/download/v28.2/protoc-28.2-linux-aarch_64.zip'); | ||
matchFile1 = true; | ||
} | ||
} | ||
assert(matchFile1); | ||
}); | ||
Comment on lines
+94
to
+130
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. 🛠️ Refactor suggestion Consider refactoring and enhancing the test case. While the new test case for fetching protobuf is functional, there are several areas for improvement:
Here's a suggested refactoring to address these points: function testGithubBinaryFetch(repoName: string, fixtureName: string, expectedDir: string, expectedFiles: Array<{name: string, date: string, size: number, url: string}>) {
it(`should fetch ${repoName}`, async () => {
const response = await TestUtil.readJSONFile(TestUtil.getFixtures(fixtureName));
app.mockHttpclient(new RegExp(`https://api.github.com/repos/.+/${repoName}/releases`), 'GET', {
data: response,
status: 200,
});
let result = await binary.fetch('/', repoName);
assert(result && result.items.length > 0);
assert(result.items.some(item => item.name === expectedDir && item.isDir));
result = await binary.fetch(`/${expectedDir}`, repoName);
assert(result?.items.every(item => !/{.*}/.test(item.url)));
result = await binary.fetch(`/${expectedDir}`, repoName);
assert(result && result.items.length > 0);
for (const expectedFile of expectedFiles) {
const matchingFile = result.items.find(item => item.name === expectedFile.name);
assert(matchingFile, `File ${expectedFile.name} not found`);
assert(matchingFile.date === expectedFile.date);
assert(matchingFile.size === expectedFile.size);
assert.equal(matchingFile.url, expectedFile.url);
}
});
}
// Usage
testGithubBinaryFetch(
'protobuf',
'protobuf-releases.json',
'v28.2/',
[
{
name: 'protoc-28.2-linux-aarch_64.zip',
date: '2024-09-18T21:02:40Z',
size: 3218760,
url: 'https://github.com/protocolbuffers/protobuf/releases/download/v28.2/protoc-28.2-linux-aarch_64.zip'
},
// Add more expected files here
]
);
// Add error case test
it('should handle API failure', async () => {
app.mockHttpclient(/https:\/\/api\.github\.com\/repos\/protocolbuffers\/protobuf\/releases/, 'GET', {
status: 500,
});
await assert.rejects(async () => {
await binary.fetch('/', 'protobuf');
}, /API request failed/);
}); This refactoring addresses the code duplication, allows for easier addition of more test cases, and includes an example of error case testing. |
||
}); | ||
}); |
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.
🛠️ Refactor suggestion
Enhance test coverage by checking multiple files.
The current test only verifies the presence and properties of a single file (
protoc-28.2-linux-aarch_64.zip
). To improve test coverage and ensure the fetch method works correctly for various file types, consider adding checks for additional files with different architectures or operating systems.Here's an example of how you could expand the test:
This approach allows you to easily add more files to check without duplicating assertion code.