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

Fix conformance results #194

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/ninety-frogs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@commonalityco/utils-conformance": patch
---

Fixes an issue where if there were no tags in the project and checks were configured to be run on all packages, no conformance results would be shown.
6 changes: 3 additions & 3 deletions apps/documentation/components/bento-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export function BentoSection() {
Discover <span className="italic">frustration-free</span> monorepos
</Balancer>
</h2>
<p className="max-w-xl mx-auto text-muted-foreground text-base md:text-lg font-medium">
<p className="max-w-3xl mx-auto text-muted-foreground text-base md:text-lg font-medium">
<Balancer>
Commonality helps you tame the chaos that comes with multi-package
workspaces and growing package ecosystems.
Automate away the tedious maintenance of growing monorepos and
package ecosystems.
</Balancer>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('getConformanceResults', () => {
expect(results[0].package).toEqual(packages[0]);
});

it('should return valid results when tests are valid', async () => {
it('should return valid results when checks are valid', async () => {
const conformersByPattern: ProjectConfig['checks'] = {
'*': [
{
Expand Down Expand Up @@ -114,6 +114,42 @@ describe('getConformanceResults', () => {
expect(results[0].package).toEqual(packages[0]);
});

it('should return valid results when checks are valid and there are no tags', async () => {
const conformersByPattern: ProjectConfig['checks'] = {
'*': [
{
name: 'ValidWorkspaceConformer',
validate: () => true,
message: 'Valid workspace',
},
],
};
const rootDirectory = '';
const packages: Package[] = [
{
path: '/path/to/workspace',
name: 'pkg-a',
version: '1.0.0',
type: PackageType.NODE,
},
];
const tagsData: TagsData[] = [];

const results = await getConformanceResults({
conformersByPattern,
rootDirectory,
packages,
tagsData,
codeownersData: [],
});

expect(results).toHaveLength(1);
expect(results[0].status).toBe(Status.Pass);
expect(results[0].message.title).toBe('Valid workspace');
expect(results[0].filter).toBe('*');
expect(results[0].package).toEqual(packages[0]);
});

it('should handle exceptions during validation', async () => {
const conformersByPattern: ProjectConfig['checks'] = {
'*': [
Expand Down
219 changes: 136 additions & 83 deletions packages/conformance/utils-conformance/src/get-conformance-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,125 @@ export type ConformanceResult = {
message: Message;
};

const filterPackages = ({
packages,
tagsData,
matchingPattern,
}: {
packages: Package[];
tagsData: TagsData[];
matchingPattern: string;
}): Package[] => {
if (matchingPattern === '*') return packages;

return packages.filter((pkg) => {
const tagsDataForPackage = tagsData.find(
(data) => data.packageName === pkg.name,
);

return tagsDataForPackage?.tags.includes(matchingPattern);
});
};

const getStatus = async ({
conformer,
rootDirectory,
pkg,
tagsMap,
codeownersMap,
packages,
}: {
conformer: Check;
rootDirectory: string;
pkg: Package;
tagsMap: Map<string, string[]>;
codeownersMap: Map<string, string[]>;
packages: Package[];
}): Promise<Status> => {
try {
const result = await conformer.validate({
package: Object.freeze({
path: path.join(rootDirectory, pkg.path),
relativePath: pkg.path,
}),
allPackages: packages.map((innerPkg) => ({
path: path.join(rootDirectory, innerPkg.path),
relativePath: innerPkg.path,
})),
rootPackage: {
path: rootDirectory,
relativePath: '.',
},
tags: tagsMap.get(pkg.name as string) ?? [],
codeowners: codeownersMap.get(pkg.name as string) ?? [],
});

if (result) {
return Status.Pass;
} else {
return conformer.level === 'error' ? Status.Fail : Status.Warn;
}
} catch {
return Status.Fail;
}
};

const getMessage = async ({
conformer,
rootDirectory,
pkg,
tagsMap,
codeownersMap,
packages,
}: {
conformer: Check;
rootDirectory: string;
pkg: Package;
tagsMap: Map<string, string[]>;
codeownersMap: Map<string, string[]>;
packages: Package[];
}): Promise<Message> => {
if (typeof conformer.message === 'string') {
return { title: conformer.message };
}

try {
const message = await conformer.message({
package: Object.freeze({
path: path.join(rootDirectory, pkg.path),
relativePath: pkg.path,
}),
allPackages: packages.map((innerPkg) => ({
path: path.join(rootDirectory, innerPkg.path),
relativePath: innerPkg.path,
})),
rootPackage: {
path: rootDirectory,
relativePath: '.',
},
tags: tagsMap.get(pkg.name as string) ?? [],
codeowners: codeownersMap.get(pkg.name as string) ?? [],
});

return {
...message,
filePath: message.filePath ?? pkg.path,
} satisfies Message;
} catch (error) {
if (error instanceof Error) {
return {
title: error.message,
filePath: pkg.path,
suggestion: error.stack,
} satisfies Message;
}

return {
title: 'An unknown error occurred while running this conformer',
};
}
};

export const getConformanceResults = async ({
conformersByPattern,
packages,
Expand All @@ -30,7 +149,6 @@ export const getConformanceResults = async ({
codeownersData: CodeownersData[];
}): Promise<ConformanceResult[]> => {
const filters = Object.keys(conformersByPattern);
const packagesMap = new Map(packages.map((pkg) => [pkg.name, pkg]));
const tagsMap = new Map(
tagsData.map((data) => [data.packageName, data.tags]),
);
Expand All @@ -41,91 +159,26 @@ export const getConformanceResults = async ({
return await Promise.all(
filters.flatMap((matchingPattern) =>
conformersByPattern[matchingPattern].flatMap((conformer) =>
tagsData
.filter((data) => {
if (matchingPattern === '*') return true;
return data.tags.includes(matchingPattern);
})
.map((data) => packagesMap.get(data.packageName))
filterPackages({ packages, tagsData, matchingPattern })
.filter((pkg): pkg is Package => !!pkg)
.map(async (pkg): Promise<ConformanceResult> => {
const getStatus = async (): Promise<Status> => {
try {
const result = await conformer.validate({
package: Object.freeze({
path: path.join(rootDirectory, pkg.path),
relativePath: pkg.path,
}),
allPackages: packages.map((innerPkg) => ({
path: path.join(rootDirectory, innerPkg.path),
relativePath: innerPkg.path,
})),
rootPackage: {
path: rootDirectory,
relativePath: '.',
},
tags: tagsMap.get(pkg.name as string) ?? [],
codeowners: codeownersMap.get(pkg.name as string) ?? [],
});

if (result) {
return Status.Pass;
} else {
return conformer.level === 'error'
? Status.Fail
: Status.Warn;
}
} catch {
return Status.Fail;
}
};

const getMessage = async () => {
if (typeof conformer.message === 'string') {
return { title: conformer.message };
}

try {
const message = await conformer.message({
package: Object.freeze({
path: path.join(rootDirectory, pkg.path),
relativePath: pkg.path,
}),
allPackages: packages.map((innerPkg) => ({
path: path.join(rootDirectory, innerPkg.path),
relativePath: innerPkg.path,
})),
rootPackage: {
path: rootDirectory,
relativePath: '.',
},
tags: tagsMap.get(pkg.name as string) ?? [],
codeowners: codeownersMap.get(pkg.name as string) ?? [],
});

return {
...message,
filePath: message.filePath ?? pkg.path,
} satisfies Message;
} catch (error) {
if (error instanceof Error) {
return {
title: error.message,
filePath: pkg.path,
suggestion: error.stack,
} satisfies Message;
}

return {
title:
'An unknown error occured while running this conformer',
};
}
};

const status = await getStatus();
const status = await getStatus({
conformer,
rootDirectory,
pkg,
tagsMap,
codeownersMap,
packages,
});

const message = await getMessage();
const message = await getMessage({
conformer,
rootDirectory,
pkg,
tagsMap,
codeownersMap,
packages,
});

return {
status,
Expand Down
Loading