Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aramovic79 committed Nov 11, 2024
1 parent 6cdf905 commit 2820f9e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
6 changes: 3 additions & 3 deletions __tests__/ordCdsrc.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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;
});

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;
Expand Down
12 changes: 6 additions & 6 deletions __tests__/ordPackageJson.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
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;
});

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"));

});

Expand Down
2 changes: 1 addition & 1 deletion __tests__/unittest/extendOrdWithCustom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
17 changes: 9 additions & 8 deletions lib/extendOrdWithCustom.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions lib/ord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -166,7 +167,7 @@ module.exports = (csn) => {
ordDocument.eventResources = eventResources;
}

ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument);
ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument, logger);

return ordDocument;
};

0 comments on commit 2820f9e

Please sign in to comment.