Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
varney committed Oct 3, 2023
1 parent 6436514 commit ff60aaf
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 67 deletions.
3 changes: 3 additions & 0 deletions config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
],
"logging": {
"pretty": true
},
"toggle": {
"pafInCommonNamespaces": true
}
}
65 changes: 39 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
"use strict";

const namespaces = require("./lib/namespaces");
const titles = require("./lib/titles");
const schemas = require("./lib/validation-helpers/schemas");
const countryCodes = require("./lib/validation-helpers/country-codes");
const formattingHelpers = require("./lib/validation-helpers/formatting-helpers");

const stripSchemaTag = require("./lib/validation-helpers/strip-joi-schema-tags");

// helpers
const caseBodyHelper = require("./lib/helpers/case-body-helper");
const codeHelper = require("./lib/helpers/code-helper");
const toggle = require("./lib/helpers/toggle");

// utils
const email = require("./lib/utils/email");
const ftp = require("./lib/utils/ftp");
const gcpAuth = require("./lib/utils/gcp-auth");
const gcs = require("./lib/utils/gcs");
const http = require("./lib/utils/http");
const iterators = require("./lib/utils/iterators");
const PDF = require("./lib/utils/pdf");
const pdfGenerator = require("./lib/utils/pdfGenerator");
const s3 = require("./lib/utils/s3");
const ses = require("./lib/utils/ses");
const sftp = require("./lib/utils/sftp");
const streams = require("./lib/utils/streams");
const http = require("./lib/utils/http");
const swedishBankday = require("./lib/utils/swedish-bankday");

// validation helpers
const countryCodes = require("./lib/validation-helpers/country-codes");
const formattingHelpers = require("./lib/validation-helpers/formatting-helpers");
const schemas = require("./lib/validation-helpers/schemas");
const stripSchemaTag = require("./lib/validation-helpers/strip-schema-tag");

// other
const namespaces = require("./lib/namespaces");
const titles = require("./lib/titles");

// test helpers
const clone = require("./test/helpers/clone");
const fakeApi = require("./test/helpers/fake-api");
const fakeFtp = require("./test/helpers/fake-ftp");
const fakeGcpAuth = require("./test/helpers/fake-gcp-auth");
Expand All @@ -34,39 +42,44 @@ const fakeSftp = require("./test/helpers/fake-sftp");
const fileUtils = require("./test/helpers/file-utils");
const messageHelper = require("./test/helpers/message-helper");
const pdfReader = require("./test/helpers/pdfReader");
const clone = require("./test/helpers/clone");

module.exports = {
// helpers
caseBodyHelper,
codeHelper,
countryCodes,
toggle,
// utils
email,
fakeApi,
fakeFtp,
fakeGcpAuth,
fakeGcs,
fakeS3,
fakeSes,
fakeSftp,
fileUtils,
formattingHelpers,
ftp,
gcpAuth,
gcs,
http,
swedishBankday,
iterators,
messageHelper,
namespaces,
titles,
PDF,
pdfGenerator,
pdfReader,
schemas,
s3,
ses,
sftp,
streams,
swedishBankday,
// validation helpers
countryCodes,
formattingHelpers,
schemas,
stripSchemaTag,
// other
namespaces,
titles,
// test helpers
clone,
fakeApi,
fakeFtp,
fakeGcpAuth,
fakeGcs,
fakeS3,
fakeSes,
fakeSftp,
fileUtils,
messageHelper,
pdfReader,
};
12 changes: 3 additions & 9 deletions lib/helpers/toggle.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
"use strict";
const config = require("exp-config");
const { toggle: configToggles = {} } = require("exp-config");

const knownToggles = [
"pafInCommonNamespaces",
];

knownToggles.sort();

config.toggle = config.toggle || {};
const knownToggles = Object.keys(configToggles).sort();

/* c8 ignore start */
function toggle(name) {
Expand All @@ -18,7 +12,7 @@ function toggle(name) {
if (process.env["NODE-DISABLE-TOGGLE"] === name) return false;
return true;
}
const value = config.toggle[name];
const value = configToggles[name];
return value === true || value === "true";
}
/* c8 ignore stop */
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/validation-helpers/schemas-test-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const stripSchemaTag = require("../../../lib/validation-helpers/strip-joi-schema-tags");
const stripSchemaTag = require("../../../lib/validation-helpers/strip-schema-tag");

const { addressSchema } = require("../../../lib/validation-helpers/schemas");

Expand Down
107 changes: 77 additions & 30 deletions test/feature/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,80 @@
const common = require("../../index");
const fs = require("fs");
const path = require("path");
// TODO: SIVA export toggle?
const paths = [ "lib", "lib/helpers", "lib/utils", "lib/validation-helpers", "test/helpers" ];

const paths = [ "lib", "test/helpers" ];
const allExports = [];
for (const basePath of paths) {
const normalizedPath = path.join(__dirname, "..", "..", basePath);
fs.readdirSync(normalizedPath).forEach((file) => {
const filePath = path.join(normalizedPath, file);
const stats = fs.statSync(filePath);
// TODO: SIVA traverse subdirectories too, instead of specifying them explicitly
if (stats.isDirectory()) return; // don't traverse subdirectories
let importName;
switch (file) {
case "pdf.js":
importName = "PDF";
break;
case "strip-joi-schema-tags.js":
importName = "stripSchemaTag";
break;
default:
importName = toCamelCase(file.replace(".js", ""));
break;
}
allExports.push(importName);
});
allExports.push(...getPathExports(basePath));
}

const expectedExports = [
// helpers
"caseBodyHelper",
"codeHelper",
"toggle",
// utils
"email",
"ftp",
"gcpAuth",
"gcs",
"http",
"iterators",
"PDF",
"pdfGenerator",
"s3",
"ses",
"sftp",
"streams",
"swedishBankday",
// validation helpers
"countryCodes",
"formattingHelpers",
"schemas",
"stripSchemaTag",
// other
"namespaces",
"titles",
// test helpers
"clone",
"fakeApi",
"fakeFtp",
"fakeGcpAuth",
"fakeGcs",
"fakeS3",
"fakeSes",
"fakeSftp",
"fileUtils",
"messageHelper",
"pdfReader",
];

describe("Exposed features", () => {
const exports = [];
const exposedExports = [];
for (const c in common) {
exports.push(c.toString());
exposedExports.push(c.toString());
}

describe("everything exported as lu-common is actually exported", () => {
for (const actualExport of exports) {
it(`should export ${actualExport}`, () => {
allExports.should.include(actualExport);
describe("everything we expect to export is exposed", () => {
for (const expectedExport of expectedExports) {
it(`${expectedExport} should be exposed`, () => {
exposedExports.should.include(expectedExport);
});
}
});

describe("everything exported is exposed as lu-common", () => {
describe("everything we expose is expected", () => {
for (const exposedExport of exposedExports) {
it(`${exposedExport} is supposed to be exposed`, () => {
expectedExports.should.include(exposedExport);
});
}
});

describe("everything exported by modules is exposed in lu-common", () => {
for (const expectedExport of allExports) {
it(`should export ${expectedExport}`, () => {
exports.should.include(expectedExport);
exposedExports.should.include(expectedExport);
});
}
});
Expand All @@ -55,3 +85,20 @@ describe("Exposed features", () => {
function toCamelCase(fileName) {
return fileName.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
}

function getPathExports(basePath) {
const exports = [];
const normalizedPath = path.join(__dirname, "..", "..", basePath);
fs.readdirSync(normalizedPath).forEach((file) => {
const filePath = path.join(normalizedPath, file);
const stats = fs.statSync(filePath);

// get all exports from subdirectories too
if (stats.isDirectory()) exports.push(...getPathExports(path.join(basePath, file)));
else {
const importName = file === "pdf.js" ? "PDF" : toCamelCase(file.replace(".js", ""));
exports.push(importName);
}
});
return exports;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const expect = require("chai").expect;
const joi = require("joi");
const stripper = require("../../../lib/validation-helpers/strip-joi-schema-tags");
const stripper = require("../../../lib/validation-helpers/strip-schema-tag");

describe("strip joi fields without tags", () => {
describe("nested", () => {
Expand Down

0 comments on commit ff60aaf

Please sign in to comment.