A library for checking and updating copyright/license headers in a project.
This package works with Deno
JSR Score
82%
Published
8 months ago (1.2.2)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213// Copyright 2023 Chris Knight. All rights reserved. MIT license. import { assert, assertEquals, assertRejects } from "jsr:@std/assert@0.215"; import { checkCopyrightHeaders, Options, updateCopyrightHeaders, } from "./mod.ts"; Deno.test({ name: "Options are validated", async fn() { const options: Options = { extensions: [".java", ".jsx"], exclusions: ["mod.ts"], headerText: `// Copyright {TIMEFRAME} the code authors. All rights reserved. MIT license.`, rootDir: ".", firstYear: 2022, quiet: false, }; options.firstYear = 1900; await assertRejects( () => checkCopyrightHeaders(options), Error, "Invalid first year", ); await assertRejects( () => updateCopyrightHeaders(options), Error, "Invalid first year", ); options.firstYear = new Date().getFullYear() + 1; await assertRejects( () => checkCopyrightHeaders(options), Error, "Invalid first year", ); await assertRejects( () => updateCopyrightHeaders(options), Error, "Invalid first year", ); options.firstYear = 1901; options.extensions = []; await assertRejects( () => checkCopyrightHeaders(options), Error, "No extensions provided", ); await assertRejects( () => updateCopyrightHeaders(options), Error, "No extensions provided", ); options.firstYear = new Date().getFullYear(); options.extensions = [".java", ".jsx"]; options.rootDir = ""; await assertRejects( () => checkCopyrightHeaders(options), Error, "No root directory provided", ); await assertRejects( () => updateCopyrightHeaders(options), Error, "No root directory provided", ); options.rootDir = "."; options.headerText = ""; await assertRejects( () => checkCopyrightHeaders(options), Error, "No license text provided", ); await assertRejects( () => updateCopyrightHeaders(options), Error, "No license text provided", ); }, }); Deno.test({ name: "Files are checked with first year specified", async fn(t) { const license = "// Copyright 2018-{TIMEFRAME}. MIT license.".replace( "{TIMEFRAME}", new Date().getFullYear().toString(), ); const licenseOld = "// Copyright 2018-{TIMEFRAME}. MIT license.".replace( "{TIMEFRAME}", (new Date().getFullYear() - 1).toString(), ); await Deno.mkdir("testdir"); Deno.writeFileSync( "my_file.test", new TextEncoder().encode("hello world\n"), ); Deno.writeFileSync( "./testdir/should_not_be_checked.java", new TextEncoder().encode("hello world\n"), ); Deno.writeFileSync( "./testdir/my_file2.test", new TextEncoder().encode("hello world\n"), ); Deno.writeFileSync( "./testdir/my_file3.test", new TextEncoder().encode(license + "\nhello world\n"), ); Deno.writeFileSync( "./testdir/my_file4.test", new TextEncoder().encode(licenseOld + "\nhello world\n"), ); await t.step("Check files with first year specified", async () => { const options: Options = { extensions: [".test"], exclusions: [], headerText: `// Copyright {TIMEFRAME}. MIT license.`, rootDir: ".", firstYear: 2018, quiet: false, }; //Files my_file.test, my_file2.test and my_file4.test should fail assert(!await checkCopyrightHeaders(options)); //Now exlude the failing files options.exclusions = [ "**/my_file2.test", "**/my_file4.test", "my_file.test", ]; assert(await checkCopyrightHeaders(options)); }); await t.step("Update files with first year specified", async () => { const options: Options = { extensions: [".test"], exclusions: [], headerText: `// Copyright {TIMEFRAME}. MIT license.`, rootDir: ".", firstYear: 2018, quiet: false, }; await updateCopyrightHeaders(options); assert(await checkCopyrightHeaders(options)); assertEquals( Deno.readTextFileSync("my_file.test"), license + "\nhello world\n", ); }); await Deno.remove("my_file.test"); await Deno.remove("./testdir", { recursive: true }); }, }); Deno.test({ name: "Files are checked without first year specified", async fn(t) { const license = "// Copyright {TIMEFRAME}. MIT license.".replace( "{TIMEFRAME}", new Date().getFullYear().toString(), ); const licenseOld = "// Copyright {TIMEFRAME}. MIT license.".replace( "{TIMEFRAME}", (new Date().getFullYear() - 1).toString(), ); await Deno.mkdir("testdir"); Deno.writeFileSync( "my_file.test", new TextEncoder().encode("hello world\n"), ); Deno.writeFileSync( "./testdir/my_file2.test", new TextEncoder().encode(license + "\nhello world\n"), ); Deno.writeFileSync( "./testdir/my_file3.test", new TextEncoder().encode(licenseOld + "\nhello world\n"), ); await t.step( "Check and update files with first year specified", async () => { const options: Options = { extensions: [".test"], exclusions: [], headerText: `// Copyright {TIMEFRAME}. MIT license.`, rootDir: ".", quiet: true, }; assert(!await checkCopyrightHeaders(options)); await updateCopyrightHeaders(options); assert(await checkCopyrightHeaders(options)); assertEquals( Deno.readTextFileSync("my_file.test"), license + "\nhello world\n", ); }, ); await Deno.remove("my_file.test"); await Deno.remove("./testdir", { recursive: true }); }, });