Skip to content
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 tests for build tools script #3245

Merged
merged 27 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ef5e32e
setup
vishvamsinh28 Sep 21, 2024
fdddfe1
added tests for errors
vishvamsinh28 Sep 21, 2024
23ba7b0
added tests for errors
vishvamsinh28 Sep 21, 2024
94d388a
build-tools tests added
vishvamsinh28 Sep 21, 2024
2397e01
testCache update
vishvamsinh28 Sep 21, 2024
5e9da70
fixtures updated
vishvamsinh28 Sep 21, 2024
c858834
fixtures updated
vishvamsinh28 Sep 21, 2024
c816f66
fixtures updated
vishvamsinh28 Sep 21, 2024
0c4126f
fefe
vishvamsinh28 Sep 21, 2024
6aa3677
Merge branch 'master' into BuildToolsTest
vishvamsinh28 Sep 23, 2024
8a9ba65
paths update
vishvamsinh28 Sep 23, 2024
2214648
Merge branch 'master' into BuildToolsTest
vishvamsinh28 Sep 25, 2024
11a5d05
added expect statements
vishvamsinh28 Sep 25, 2024
9e29a5a
Merge branch 'master' into BuildToolsTest
vishvamsinh28 Sep 26, 2024
aa91e09
Merge branch 'BuildToolsTest' of https://github.com/vishvamsinh28/web…
vishvamsinh28 Sep 26, 2024
26a4be2
tests updated
vishvamsinh28 Sep 26, 2024
6592626
Merge branch 'master' into BuildToolsTest
vishvamsinh28 Sep 28, 2024
b3b34f8
Merge branch 'master' into BuildToolsTest
vishvamsinh28 Sep 30, 2024
b744e49
fef
vishvamsinh28 Oct 2, 2024
9d00110
fef
vishvamsinh28 Oct 2, 2024
7b8a407
fefwf
vishvamsinh28 Oct 2, 2024
0c4efb2
fwqwf
vishvamsinh28 Oct 2, 2024
61b3e16
fwaf
vishvamsinh28 Oct 2, 2024
e99f5cf
fwfw
vishvamsinh28 Oct 2, 2024
5cbe440
test updated
vishvamsinh28 Oct 2, 2024
bdf13cf
Merge branch 'master' into BuildToolsTest
vishvamsinh28 Oct 2, 2024
4600250
remove redundant tests
anshgoyalevil Oct 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions scripts/build-tools.js
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);
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
console.log(err);
throw err
throw new Error(`An error occurred while building tools: ${err.message}`);
}
};

buildTools();
/* istanbul ignore next */
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
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 };
13 changes: 3 additions & 10 deletions scripts/tools/combine-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const fs = require('fs')
const schema = require("./tools-schema.json");
const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const { resolve } = require('path');
const Fuse = require("fuse.js");
const ajv = new Ajv()
addFormats(ajv, ["uri"])
Expand Down Expand Up @@ -106,7 +105,7 @@ const getFinalTool = async (toolObject) => {

// Combine the automated tools and manual tools list into single JSON object file, and
// lists down all the language and technology tags in one JSON file.
const combineTools = async (automatedTools, manualTools) => {
const combineTools = async (automatedTools, manualTools, toolsPath, tagsPath) => {
for (const key in automatedTools) {
let finalToolsList = [];
if (automatedTools[key].toolsList.length) {
Expand Down Expand Up @@ -136,14 +135,8 @@ const combineTools = async (automatedTools, manualTools) => {
finalToolsList.sort((tool, anotherTool) => tool.title.localeCompare(anotherTool.title));
finalTools[key].toolsList = finalToolsList
}
fs.writeFileSync(
resolve(__dirname, '../../config', 'tools.json'),
JSON.stringify(finalTools)
);
fs.writeFileSync(
resolve(__dirname, '../../config', 'all-tags.json'),
JSON.stringify({ languages: languageList, technologies: technologyList }),
)
fs.writeFileSync(toolsPath,JSON.stringify(finalTools));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does final tools signify??

fs.writeFileSync(tagsPath,JSON.stringify({ languages: languageList, technologies: technologyList }),)
}

module.exports = { combineTools }
112 changes: 112 additions & 0 deletions tests/build-tools.test.js
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const { readFileSync, rmSync, mkdirSync, writeFileSync } = require('fs');
const { resolve } = require('path');
const { getData } = require('../scripts/tools/extract-tools-github');
const { convertTools } = require('../scripts/tools/tools-object');
const { combineTools } = require('../scripts/tools/combine-tools');
const { buildTools } = require('../scripts/build-tools');
const { tagsData, manualTools, mockConvertedData, initialToolsData, mockExtractData } = require('../tests/fixtures/buildToolsData');

jest.mock('../scripts/tools/extract-tools-github');
jest.mock('../scripts/tools/tools-object');
jest.mock('../scripts/tools/combine-tools');

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(() => {
mkdirSync(testDir, { recursive: true });

writeFileSync(manualToolsPath, JSON.stringify(manualTools));
});

afterAll(() => {
rmSync(testDir, { recursive: true, force: true });
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should extract, convert, combine tools, and write to file', async () => {
getData.mockImplementation(async () => {
writeFileSync(automatedToolsPath, JSON.stringify(mockExtractData));
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
return mockExtractData;
});

convertTools.mockImplementation(async () => {
writeFileSync(automatedToolsPath, JSON.stringify(mockConvertedData));
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
return mockConvertedData;
});

combineTools.mockImplementation(async () => {
writeFileSync(toolsPath, JSON.stringify(initialToolsData));
writeFileSync(tagsPath, JSON.stringify(tagsData));
return true;
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
});

await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);

const automatedToolsContent = readFileSync(automatedToolsPath, 'utf8');
expect(JSON.parse(automatedToolsContent)).toEqual(mockConvertedData);

const manualToolsData = JSON.parse(readFileSync(manualToolsPath, 'utf8'));
const tagsFileData = JSON.parse(readFileSync(tagsPath, 'utf8'));
const toolsFileData = JSON.parse(readFileSync(toolsPath, 'utf8'));

expect(combineTools).toHaveBeenCalledWith(mockConvertedData, manualToolsData, toolsPath, tagsPath);

expect(toolsFileData).toEqual(initialToolsData);
expect(tagsFileData).toEqual(tagsData);
});

it('should handle getData error', async () => {
getData.mockRejectedValue(new Error('Extract error'));

try {
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);
} catch (err) {
expect(err.message).toContain('Extract error');
}
});

it('should handle convertTools error', async () => {
getData.mockResolvedValue(mockExtractData);
convertTools.mockRejectedValue(new Error('Convert error'));

try {
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);
} catch (err) {
expect(err.message).toContain('Convert error');
}
});

it('should handle combineTools error', async () => {
getData.mockResolvedValue(mockExtractData);
convertTools.mockResolvedValue(mockConvertedData);
combineTools.mockRejectedValue(new Error('Combine Tools error'));

try {
await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath);
} catch (err) {
expect(err.message).toContain('Combine Tools error');
}
});

it('should handle file write errors', async () => {
getData.mockResolvedValue(mockExtractData);
convertTools.mockResolvedValue(mockConvertedData);
combineTools.mockResolvedValue(true);

const invalidPath = '/invalid_dir/tools.json';

try {
await buildTools(invalidPath, manualToolsPath, toolsPath, tagsPath);
} catch (err) {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});
});
39 changes: 39 additions & 0 deletions tests/fixtures/buildToolsData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const tagsData = [
{ id: 1, name: 'tag1' },
{ id: 2, name: 'tag2' },
];

const manualTools = [
{ id: 1, tool: 'manualTool1' },
{ id: 2, tool: 'manualTool2' },
];

const mockConvertedData = [
{ id: 1, tool: 'tool1' },
{ id: 2, tool: 'tool2' },
];

const initialToolsData = [
{
title: "API Tracker",
description: "Explore public AsyncAPI specifications.",
},
{
title: "AsyncAPI Server API",
description: "Official tools for AsyncAPI.",
},
{
title: "AsyncAPI Generator",
description: "Generate AsyncAPI documents effortlessly.",
},
];

const mockExtractData = [{ name: 'tool1' }, { name: 'tool2' }];

module.exports = {
tagsData,
manualTools,
mockConvertedData,
initialToolsData,
mockExtractData,
};
Loading