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 9 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');
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
const tagsPath = resolve(__dirname, '../../config', 'all-tags.json');

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

module.exports = { buildTools };
106 changes: 106 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,106 @@
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(tagsPath, JSON.stringify(tagsData));
writeFileSync(automatedToolsPath, JSON.stringify(mockConvertedData));
writeFileSync(manualToolsPath, JSON.stringify(manualTools));
writeFileSync(toolsPath, JSON.stringify(initialToolsData));
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
});

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

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

it('should extract, convert, combine tools, and write to file', async () => {

getData.mockResolvedValue(mockExtractData);
convertTools.mockResolvedValue(mockConvertedData);
combineTools.mockResolvedValue(true);

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 tagsData = JSON.parse(readFileSync(tagsPath, 'utf8'));
expect(combineTools).toHaveBeenCalledWith(mockConvertedData, manualToolsData, toolsPath, tagsPath);

const toolsContent = readFileSync(toolsPath, 'utf8');
expect(toolsContent).toBeDefined();
expect(tagsData).toBeDefined();
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
});

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