-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests for build tools script (#3245)
Co-authored-by: Ansh Goyal <[email protected]>
- Loading branch information
1 parent
1ae0ca3
commit e56289b
Showing
4 changed files
with
193 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
const { getData } = require('./tools/extract-tools-github'); | ||
const { convertTools } = require('./tools/tools-object'); | ||
const { combineTools } = require('./tools/combine-tools'); | ||
const manualTools = require('../config/tools-manual.json') | ||
|
||
const fs = require('fs'); | ||
const { resolve } = require('path'); | ||
|
||
const buildTools = async () => { | ||
const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPath) => { | ||
try { | ||
let githubExtractData = await getData(); | ||
let automatedTools = await convertTools(githubExtractData); | ||
|
||
fs.writeFileSync( | ||
resolve(__dirname, '../config', 'tools-automated.json'), | ||
automatedToolsPath, | ||
JSON.stringify(automatedTools, null, ' ') | ||
); | ||
await combineTools(automatedTools, manualTools); | ||
|
||
await combineTools(automatedTools, require(manualToolsPath), toolsPath, tagsPath); | ||
} catch (err) { | ||
console.log(err); | ||
throw err | ||
throw new Error(`An error occurred while building tools: ${err.message}`); | ||
} | ||
}; | ||
|
||
buildTools(); | ||
/* istanbul ignore next */ | ||
if (require.main === module) { | ||
const automatedToolsPath = resolve(__dirname, '../config', 'tools-automated.json'); | ||
const manualToolsPath = resolve(__dirname, '../config', 'tools-manual.json'); | ||
const toolsPath = resolve(__dirname, '../config', 'tools.json'); | ||
const tagsPath = resolve(__dirname, '../config', 'all-tags.json'); | ||
|
||
buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath); | ||
} | ||
|
||
module.exports = { buildTools }; |
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,89 @@ | ||
const axios = require('axios'); | ||
const { resolve } = require('path'); | ||
const { buildTools } = require('../scripts/build-tools'); | ||
const { tagsData, manualTools, mockConvertedData, mockExtractData } = require('../tests/fixtures/buildToolsData'); | ||
const fs = require('fs'); | ||
|
||
jest.mock('axios'); | ||
jest.mock('../scripts/tools/categorylist', () => ({ | ||
categoryList: [ | ||
{ name: 'Category1', description: 'Description for Category1' }, | ||
{ name: 'Category2', description: 'Description for Category2' } | ||
] | ||
})); | ||
|
||
jest.mock('../scripts/tools/tags-color', () => ({ | ||
languagesColor: [ | ||
{ name: 'JavaScript', color: 'bg-[#f1e05a]', borderColor: 'border-[#f1e05a]' }, | ||
{ name: 'Python', color: 'bg-[#3572A5]', borderColor: 'border-[#3572A5]' } | ||
], | ||
technologiesColor: [ | ||
{ name: 'React', color: 'bg-[#61dafb]', borderColor: 'border-[#61dafb]' }, | ||
{ name: 'Node.js', color: 'bg-[#68a063]', borderColor: 'border-[#68a063]' } | ||
] | ||
})); | ||
|
||
describe('buildTools', () => { | ||
const testDir = resolve(__dirname, 'test_config'); | ||
const toolsPath = resolve(testDir, 'tools.json'); | ||
const tagsPath = resolve(testDir, 'all-tags.json'); | ||
const automatedToolsPath = resolve(testDir, 'tools-automated.json'); | ||
const manualToolsPath = resolve(testDir, 'tools-manual.json'); | ||
|
||
beforeAll(() => { | ||
fs.mkdirSync(testDir, { recursive: true }); | ||
fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); | ||
}); | ||
|
||
afterAll(() => { | ||
fs.rmSync(testDir, { recursive: true, force: true }); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should extract, convert, combine tools, and write to file', async () => { | ||
axios.get.mockResolvedValue({ data: mockExtractData }); | ||
|
||
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath); | ||
|
||
const automatedToolsContent = JSON.parse(fs.readFileSync(automatedToolsPath, 'utf8')); | ||
const combinedToolsContent = JSON.parse(fs.readFileSync(toolsPath, 'utf8')); | ||
const tagsContent = JSON.parse(fs.readFileSync(tagsPath, 'utf8')); | ||
|
||
expect(Object.keys(automatedToolsContent)).toEqual(Object.keys(mockConvertedData)); | ||
expect(automatedToolsContent["Category1"].description).toEqual(mockConvertedData["Category1"].description); | ||
expect(automatedToolsContent["Category2"].description).toEqual(mockConvertedData["Category2"].description); | ||
|
||
expect(combinedToolsContent).toHaveProperty('Category1'); | ||
expect(combinedToolsContent).toHaveProperty('Category2'); | ||
expect(combinedToolsContent["Category1"].description).toEqual(mockConvertedData["Category1"].description); | ||
expect(combinedToolsContent["Category2"].description).toEqual(mockConvertedData["Category2"].description); | ||
|
||
expect(tagsContent).toEqual(tagsData); | ||
|
||
}); | ||
|
||
it('should handle getData error', async () => { | ||
axios.get.mockRejectedValue(new Error('Extract error')); | ||
|
||
try { | ||
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath); | ||
} catch (err) { | ||
expect(err.message).toContain('Extract error'); | ||
} | ||
}); | ||
|
||
it('should handle file write errors', async () => { | ||
axios.get.mockResolvedValue({ data: mockExtractData }); | ||
|
||
const invalidPath = '/invalid_dir/tools.json'; | ||
|
||
try { | ||
await buildTools(invalidPath, manualToolsPath, toolsPath, tagsPath); | ||
} catch (err) { | ||
expect(err.message).toMatch(/ENOENT|EACCES/); | ||
} | ||
}); | ||
}); |
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,84 @@ | ||
const tagsData = { | ||
languages: [ | ||
{ name: 'JavaScript', color: 'bg-[#f1e05a]', borderColor: 'border-[#f1e05a]' }, | ||
{ name: 'Python', color: 'bg-[#3572A5]', borderColor: 'border-[#3572A5]' }, | ||
], | ||
technologies: [ | ||
{ name: 'React', color: 'bg-[#61dafb]', borderColor: 'border-[#61dafb]' }, | ||
{ name: 'Node.js', color: 'bg-[#68a063]', borderColor: 'border-[#68a063]' }, | ||
], | ||
}; | ||
|
||
const manualTools = [ | ||
{ | ||
title: "Manual Tool 1", | ||
description: "Description for manual tool 1", | ||
links: { repoUrl: "https://github.com/manual/tool1" }, | ||
filters: { categories: ["Category1"], language: "JavaScript", technology: ["React"] } | ||
}, | ||
{ | ||
title: "Manual Tool 2", | ||
description: "Description for manual tool 2", | ||
links: { repoUrl: "https://github.com/manual/tool2" }, | ||
filters: { categories: ["Category2"], language: "Python", technology: ["Node.js"] } | ||
}, | ||
]; | ||
|
||
const mockConvertedData = { | ||
"Category1": { | ||
description: "Description for Category1", | ||
toolsList: [ | ||
{ | ||
title: "Tool 1", | ||
description: "Description for tool 1", | ||
links: { repoUrl: "https://github.com/tool1" }, | ||
filters: { categories: ["Category1"], language: "JavaScript", technology: ["React"] } | ||
} | ||
] | ||
}, | ||
"Category2": { | ||
description: "Description for Category2", | ||
toolsList: [ | ||
{ | ||
title: "Tool 2", | ||
description: "Description for tool 2", | ||
links: { repoUrl: "https://github.com/tool2" }, | ||
filters: { categories: ["Category2"], language: "Python", technology: ["Node.js"] } | ||
} | ||
] | ||
} | ||
}; | ||
|
||
const mockExtractData = { | ||
items: [ | ||
{ | ||
name: '.asyncapi-tool', | ||
url: 'https://api.github.com/repositories/123456/contents/.asyncapi-tool?ref=abcdef', | ||
repository: { | ||
full_name: 'user/repo1', | ||
html_url: 'https://github.com/user/repo1', | ||
description: 'Description for repo1', | ||
owner: { login: 'asyncapi' } | ||
}, | ||
path: '.asyncapi-tool' | ||
}, | ||
{ | ||
name: '.asyncapi-tool', | ||
url: 'https://api.github.com/repositories/789012/contents/.asyncapi-tool?ref=ghijkl', | ||
repository: { | ||
full_name: 'user/repo2', | ||
html_url: 'https://github.com/user/repo2', | ||
description: 'Description for repo2', | ||
owner: { login: 'asyncapi' } | ||
}, | ||
path: '.asyncapi-tool' | ||
} | ||
] | ||
}; | ||
|
||
module.exports = { | ||
tagsData, | ||
manualTools, | ||
mockConvertedData, | ||
mockExtractData, | ||
}; |