From 2820f9eb99505b6413a09f6d838d21cbd92f1090 Mon Sep 17 00:00:00 2001 From: Albin Ramovic Date: Mon, 11 Nov 2024 17:08:18 +0100 Subject: [PATCH] address review comments --- __tests__/ordCdsrc.test.js | 6 +++--- __tests__/ordPackageJson.test.js | 12 ++++++------ __tests__/unittest/extendOrdWithCustom.test.js | 2 +- lib/extendOrdWithCustom.js | 17 +++++++++-------- lib/ord.js | 11 ++++++----- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/__tests__/ordCdsrc.test.js b/__tests__/ordCdsrc.test.js index 53b8f80..dbf2e0e 100644 --- a/__tests__/ordCdsrc.test.js +++ b/__tests__/ordCdsrc.test.js @@ -1,11 +1,12 @@ +const cds = require("@sap/cds"); const ord = require("../lib/ord"); const path = require("path"); // Mock the @sap/cds module jest.mock("@sap/cds", () => { - const { join } = require("path"); + const path = require("path"); let originalCds = jest.requireActual("@sap/cds"); - originalCds.root = join(__dirname, "bookshop"); + originalCds.root = path.join(__dirname, "bookshop"); return originalCds; }); @@ -13,7 +14,6 @@ jest.mock("../lib/date", () => ({ getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") })); -const cds = require("@sap/cds"); describe("Tests for default ORD document when .cdsrc.json is present", () => { let csn; diff --git a/__tests__/ordPackageJson.test.js b/__tests__/ordPackageJson.test.js index 32d5fa0..0b0558d 100644 --- a/__tests__/ordPackageJson.test.js +++ b/__tests__/ordPackageJson.test.js @@ -1,11 +1,13 @@ +const cds = require("@sap/cds"); const ord = require("../lib/ord"); -const { join } = require("path"); +const path = require("path"); + // Mock the @sap/cds module jest.mock("@sap/cds", () => { - const { join } = require("path"); + const path = require("path"); let originalCds = jest.requireActual("@sap/cds"); - originalCds.root = join(__dirname, "bookshop"); + originalCds.root = path.join(__dirname, "bookshop"); return originalCds; }); @@ -13,14 +15,12 @@ jest.mock("../lib/date", () => ({ getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") })); -const cds = require("@sap/cds"); - describe("Tests for default ORD document when .cdsrc.json is not present", () => { let csn; beforeAll(async () => { cds.env["ord"] = ""; - csn = await cds.load(join(__dirname, "bookshop", "srv")); + csn = await cds.load(path.join(__dirname, "bookshop", "srv")); }); diff --git a/__tests__/unittest/extendOrdWithCustom.test.js b/__tests__/unittest/extendOrdWithCustom.test.js index d293f4e..94d6dfd 100644 --- a/__tests__/unittest/extendOrdWithCustom.test.js +++ b/__tests__/unittest/extendOrdWithCustom.test.js @@ -46,7 +46,7 @@ describe('extendOrdWithCustom', () => { const ordContent = {}; const warningSpy = jest.spyOn(console, 'warn'); prepareTestEnvironment({ namespace: "sap.sample" }, appConfig, 'testCustomORDContentFileThrowErrors.json'); - const result = extendCustomORDContentIfExists(appConfig, ordContent); + const result = extendCustomORDContentIfExists(appConfig, ordContent, cds.log()); expect(warningSpy).toHaveBeenCalledTimes(3); expect(warningSpy).toHaveBeenCalledWith('Mocked warning'); diff --git a/lib/extendOrdWithCustom.js b/lib/extendOrdWithCustom.js index 969028e..981d5c2 100644 --- a/lib/extendOrdWithCustom.js +++ b/lib/extendOrdWithCustom.js @@ -1,7 +1,8 @@ -const { root, log } = require("@sap/cds"); -const { join } = require("path"); -const _ = require('lodash'); const { CONTENT_MERGE_KEY } = require('./constants'); +const cds = require("@sap/cds"); +const path = require("path"); +const _ = require('lodash'); + function cleanNullProperties(obj) { @@ -28,12 +29,12 @@ function patchGeneratedOrdResources(destinationObj, sourceObj) { return cleanNullProperties(Object.values(destObj)); } -function compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent) { +function compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent, logger) { const clonedOrdContent = structuredClone(ordContent); for (const key in customORDContent) { const propertyType = typeof customORDContent[key]; if (propertyType !== 'object' && propertyType !== 'array') { - log('ord-plugin').warn('Found ord top level primitive ord property in customOrdFile:', key, ', please define it in .cdsrc.json.'); + logger.warn('Found ord top level primitive ord property in customOrdFile:', key, ', please define it in .cdsrc.json.'); continue; } if (key in ordContent) { @@ -47,17 +48,17 @@ function compareAndHandleCustomORDContentWithExistingContent(ordContent, customO function getCustomORDContent(appConfig) { if (appConfig.env?.customOrdContentFile) { - const customORDContent = require(join(root, appConfig.env.customOrdContentFile)); + const customORDContent = require(path.join(cds.root, appConfig.env.customOrdContentFile)); return customORDContent; } return {}; } -function extendCustomORDContentIfExists(appConfig, ordContent) { +function extendCustomORDContentIfExists(appConfig, ordContent, logger) { const customORDContent = getCustomORDContent(appConfig); if (customORDContent) { - ordContent = compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent); + ordContent = compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent, logger); } return ordContent; } diff --git a/lib/ord.js b/lib/ord.js index 5f5dd34..9fe8f0d 100644 --- a/lib/ord.js +++ b/lib/ord.js @@ -5,15 +5,16 @@ const { createEventResourceTemplate, createGroupsTemplateForService } = require('./templates'); -const cds = require("@sap/cds"); -const defaults = require("./defaults"); const { extendCustomORDContentIfExists } = require('./extendOrdWithCustom'); const { getRFC3339Date } = require('./date'); const _ = require("lodash"); -const { join } = require("path"); +const cds = require("@sap/cds"); +const defaults = require("./defaults"); +const logger = cds.log('ord-plugin'); +const path = require("path"); const initializeAppConfig = (csn) => { - let packageJsonPath = join(cds.root, 'package.json') + let packageJsonPath = path.join(cds.root, 'package.json') let packageJson; if (cds.utils.exists(packageJsonPath)) { packageJson = require(packageJsonPath); @@ -166,7 +167,7 @@ module.exports = (csn) => { ordDocument.eventResources = eventResources; } - ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument); + ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument, logger); return ordDocument; };