8000 refactor(report)!: implement test & proper cleanup of report by fraxken · Pull Request #384 · NodeSecure/report · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor(report)!: implement test & proper cleanup of report #384

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

Merged
merged 1 commit into from
Jul 9, 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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,15 @@ The theme can be either `dark` or `light`. Themes are editable in _public/css/th
> [!CAUTION]
> The API is ESM only

### `report(options: ReportConfiguration, payload: Scanner.Payload): Promise<Buffer>`
### report

```ts
function report(
scannerDependencies: Scanner.Payload["dependencies"],
report: ReportConfiguration,
reportOutputLocation?: string
): Promise<Buffer>;
```

Generates and returns a PDF Buffer based on the provided report options and scanner payload.

Expand Down
47 changes: 29 additions & 18 deletions src/api/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,41 @@ import { buildStatsFromNsecurePayloads } from "../analysis/extractScannerData.js
import { HTML, PDF } from "../reporting/index.js";

export async function report(
scannerDependencies,
reportOptions,
scannerPayload
reportOutputLocation = null
) {
const [pkgStats, reportOutputLocation] = await Promise.all([
buildStatsFromNsecurePayloads(scannerPayload, {
const [pkgStats, finalReportLocation] = await Promise.all([
buildStatsFromNsecurePayloads(scannerDependencies, {
isJson: true,
reportOptions
}),
fs.mkdtemp(
path.join(os.tmpdir(), "nsecure-report-")
)
reportOutputLocation === null ?
fs.mkdtemp(path.join(os.tmpdir(), "nsecure-report-")) :
Promise.resolve(reportOutputLocation)
]);

const reportHTMLPath = await HTML(
{
pkgStats,
repoStats: null
},
reportOptions,
reportOutputLocation
);
try {
const reportHTMLPath = await HTML(
{
pkgStats,
repoStats: null
},
reportOptions,
finalReportLocation
);

return PDF(reportHTMLPath, {
title: reportOptions.title,
saveOnDisk: false
});
return await PDF(reportHTMLPath, {
title: reportOptions.title,
saveOnDisk: false
});
}
finally {
if (reportOutputLocation === null) {
await fs.rm(finalReportLocation, {
force: true,
recursive: true
});
}
}
}
3 changes: 2 additions & 1 deletion src/reporting/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export async function HTML(
npm_stats: pkgStats,
git_stats: repoStats,
charts
}
},
reportOptions
).render();

await Promise.all([
Expand Down
88 changes: 88 additions & 0 deletions test/api/report.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Import Node.js Dependencies
import path from "node:path";
import os from "node:os";
import fs from "node:fs/promises";
import { describe, test } from "node:test";
import assert from "node:assert";

// Import Third-party Dependencies
import { from } from "@nodesecure/scanner";

// Import Internal Dependencies
import { report } from "../../src/index.js";

// CONSTANTS
const kReportPayload = {
title: "test_runner",
theme: "light",
includeTransitiveInternal: false,
npm: {
organizationPrefix: null,
packages: []
},
reporters: [
"pdf"
],
charts: [
{
name: "Extensions",
display: true,
interpolation: "d3.interpolateRainbow",
type: "bar"
},
{
name: "Licenses",
display: true,
interpolation: "d3.interpolateCool",
type: "bar"
},
{
name: "Warnings",
display: true,
type: "horizontalBar",
interpolation: "d3.interpolateInferno"
},
{
name: "Flags",
display: true,
type: "horizontalBar",
interpolation: "d3.interpolateSinebow"
}
]
};

describe("(API) report", () => {
test("Given a scanner Payload it should successfully generate a PDF", async() => {
const reportLocation = await fs.mkdtemp(
path.join(os.tmpdir(), "test-runner-report-pdf-")
);

const payload = await from("sade");

const generatedPDF = await report(
payload.dependencies,
structuredClone(kReportPayload),
reportLocation
);
try {
assert.ok(Buffer.isBuffer(generatedPDF));
assert.ok(isPDF(generatedPDF));

const files = (await fs.readdir(reportLocation, { withFileTypes: true }))
.flatMap((dirent) => (dirent.isFile() ? [dirent.name] : []));
assert.deepEqual(
files,
["test_runner.html"]
);
}
finally {
await fs.rm(reportLocation, { force: true, recursive: true });
}
});
});

function isPDF(buf) {
return (
Buffer.isBuffer(buf) && buf.lastIndexOf("%PDF-") === 0 && buf.lastIndexOf("%%EOF") > -1
);
}
0