@tenonhq/dovetail-core 0.0.133 → 0.0.135
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/FileUtils.js +14 -0
- package/dist/createRecordCommand.js +18 -2
- package/dist/deleteRecordCommand.js +45 -20
- package/dist/fingerprint.js +44 -5
- package/dist/layoutElements.js +386 -0
- package/dist/reconcileCommand.js +6 -0
- package/dist/recordFolder.js +86 -0
- package/dist/snClient.js +52 -6
- package/dist/tests/createRecordLocalPath.test.js +145 -0
- package/dist/tests/deleteRecordLocalCleanup.test.js +153 -24
- package/dist/tests/fileContextSkipReason.test.js +41 -0
- package/dist/tests/fingerprint.test.js +37 -0
- package/dist/tests/layoutElements.test.js +259 -0
- package/dist/tests/layoutElementsWriteGuards.test.js +113 -0
- package/dist/tests/pushFingerprintedField.test.js +64 -0
- package/dist/tests/recordFolder.test.js +57 -0
- package/dist/tests/snClient.fingerprint.test.js +20 -0
- package/dist/tests/snClient.layoutElements.test.js +133 -0
- package/package.json +3 -3
- package/release-manifest.json +340 -347
- package/skills/dove-delete-record.md +2 -2
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Where a record lives on disk, read from the scope manifest.
|
|
4
|
+
*
|
|
5
|
+
* A manifest key IS the record's folder name (appUtils.normalizeManifestKeys,
|
|
6
|
+
* folderNaming.ts), so a command that knows a record's sys_id and needs its
|
|
7
|
+
* folder looks the key up by sys_id. Building the folder from the instance's
|
|
8
|
+
* display name instead is wrong for every record the naming rule renamed
|
|
9
|
+
* (namesakes, letter-case twins, unsafe names, nameTemplate tables), and points
|
|
10
|
+
* at ANOTHER record's folder when a local namesake exists and the record in
|
|
11
|
+
* hand has not been refreshed yet.
|
|
12
|
+
*/
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.recordDirFor = exports.recordFoldersBySysId = void 0;
|
|
18
|
+
const path_1 = __importDefault(require("path"));
|
|
19
|
+
/**
|
|
20
|
+
* Every manifest key in `table` whose record has this sys_id. Normally zero or
|
|
21
|
+
* one; more than one means a hand-edited or corrupt manifest, and a caller
|
|
22
|
+
* about to delete something should treat it as "unknown".
|
|
23
|
+
*/
|
|
24
|
+
const recordFoldersBySysId = (manifest, table, sysId) => {
|
|
25
|
+
const out = [];
|
|
26
|
+
if (!manifest || typeof manifest !== "object")
|
|
27
|
+
return out;
|
|
28
|
+
if (typeof table !== "string" || table === "")
|
|
29
|
+
return out;
|
|
30
|
+
if (typeof sysId !== "string" || sysId === "")
|
|
31
|
+
return out;
|
|
32
|
+
const tables = manifest.tables;
|
|
33
|
+
if (!tables || typeof tables !== "object")
|
|
34
|
+
return out;
|
|
35
|
+
if (!Object.prototype.hasOwnProperty.call(tables, table))
|
|
36
|
+
return out;
|
|
37
|
+
const tableEntry = tables[table];
|
|
38
|
+
if (!tableEntry || typeof tableEntry !== "object")
|
|
39
|
+
return out;
|
|
40
|
+
const records = tableEntry.records;
|
|
41
|
+
if (!records || typeof records !== "object")
|
|
42
|
+
return out;
|
|
43
|
+
const keys = Object.keys(records);
|
|
44
|
+
for (let i = 0; i < keys.length; i++) {
|
|
45
|
+
const record = records[keys[i]];
|
|
46
|
+
if (record && typeof record === "object" && record.sys_id === sysId) {
|
|
47
|
+
out.push(keys[i]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
};
|
|
52
|
+
exports.recordFoldersBySysId = recordFoldersBySysId;
|
|
53
|
+
// `child` is exactly one path segment below `parent`: not the parent itself,
|
|
54
|
+
// not above it, not two levels down, not on another drive.
|
|
55
|
+
const isDirectChild = (parent, child) => {
|
|
56
|
+
const rel = path_1.default.relative(parent, child);
|
|
57
|
+
if (rel === "" || rel === "." || rel === "..")
|
|
58
|
+
return false;
|
|
59
|
+
if (path_1.default.isAbsolute(rel))
|
|
60
|
+
return false;
|
|
61
|
+
return rel.indexOf("/") === -1 && rel.indexOf("\\") === -1;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* `<scopeDir>/<table>/<folder>`, resolved, or undefined when the result is not
|
|
65
|
+
* a record folder: `<scopeDir>/<table>` must be a direct child of `<scopeDir>`
|
|
66
|
+
* and the record folder a direct child of that. A table or folder of "", "..",
|
|
67
|
+
* "a/b" or an absolute path is refused, so a caller that deletes the result can
|
|
68
|
+
* never reach the scope folder, a sibling scope or anything outside them.
|
|
69
|
+
*/
|
|
70
|
+
const recordDirFor = (scopeDir, table, folder) => {
|
|
71
|
+
if (typeof scopeDir !== "string" || scopeDir === "")
|
|
72
|
+
return undefined;
|
|
73
|
+
if (typeof table !== "string" || table === "")
|
|
74
|
+
return undefined;
|
|
75
|
+
if (typeof folder !== "string" || folder === "")
|
|
76
|
+
return undefined;
|
|
77
|
+
const scopeRoot = path_1.default.resolve(scopeDir);
|
|
78
|
+
const tableDir = path_1.default.resolve(scopeRoot, table);
|
|
79
|
+
if (!isDirectChild(scopeRoot, tableDir))
|
|
80
|
+
return undefined;
|
|
81
|
+
const recordDir = path_1.default.resolve(tableDir, folder);
|
|
82
|
+
if (!isDirectChild(tableDir, recordDir))
|
|
83
|
+
return undefined;
|
|
84
|
+
return recordDir;
|
|
85
|
+
};
|
|
86
|
+
exports.recordDirFor = recordDirFor;
|
package/dist/snClient.js
CHANGED
|
@@ -18,6 +18,7 @@ const genericUtils_1 = require("./genericUtils");
|
|
|
18
18
|
const Logger_1 = require("./Logger");
|
|
19
19
|
const FileLogger_1 = require("./FileLogger");
|
|
20
20
|
const fingerprint_1 = require("./fingerprint");
|
|
21
|
+
const layoutElements_1 = require("./layoutElements");
|
|
21
22
|
const authFailure_1 = require("./authFailure");
|
|
22
23
|
// Benchmark sink. Null when --benchmark is off; zero overhead in that case.
|
|
23
24
|
let _benchmarkSink = null;
|
|
@@ -606,20 +607,54 @@ const snClient = (baseURL, username, password, apiKey) => {
|
|
|
606
607
|
user: userSysId,
|
|
607
608
|
});
|
|
608
609
|
};
|
|
610
|
+
// Rows of one table through the Table API, raw values, reference links left
|
|
611
|
+
// out. Used for the child rows of layouts (layoutElements.ts); the table, the
|
|
612
|
+
// fields and the query are built there from constants and checked record ids.
|
|
613
|
+
const readTableRows = async (table, query, fields, limit, offset) => {
|
|
614
|
+
const endpoint = "api/now/table/" + encodeURIComponent(table);
|
|
615
|
+
const resp = await client.get(endpoint, {
|
|
616
|
+
params: {
|
|
617
|
+
sysparm_query: query,
|
|
618
|
+
sysparm_fields: fields.join(","),
|
|
619
|
+
sysparm_limit: String(limit),
|
|
620
|
+
sysparm_offset: String(offset),
|
|
621
|
+
sysparm_exclude_reference_link: "true",
|
|
622
|
+
sysparm_display_value: "false",
|
|
623
|
+
// With a long IN list the instance cannot build its pagination Link
|
|
624
|
+
// header and answers HTTP 400 "Pagination not supported". Paging is
|
|
625
|
+
// done here by limit and offset, so the header is not needed.
|
|
626
|
+
sysparm_suppress_pagination_header: "true",
|
|
627
|
+
},
|
|
628
|
+
});
|
|
629
|
+
const rows = resp && resp.data ? resp.data.result : undefined;
|
|
630
|
+
if (!Array.isArray(rows)) {
|
|
631
|
+
throw new Error("Unexpected response reading " + table + " (no result array)");
|
|
632
|
+
}
|
|
633
|
+
return rows;
|
|
634
|
+
};
|
|
635
|
+
// A client-only table option that is set but malformed (fingerprint,
|
|
636
|
+
// layoutElements) is ignored and reported, never guessed at.
|
|
637
|
+
const warnTableOption = (message) => {
|
|
638
|
+
Logger_1.logger.warn(message);
|
|
639
|
+
};
|
|
609
640
|
const getMissingFiles = (missingFiles, tableOptions) => {
|
|
610
641
|
const endpoint = `api/cadso/dovetail_sync/bulkDownload`;
|
|
611
642
|
const tableCount = Object.keys(missingFiles).length;
|
|
612
643
|
FileLogger_1.fileLogger.debug("Bulk downloading files for " + tableCount + " tables");
|
|
613
|
-
//
|
|
614
|
-
//
|
|
644
|
+
// A layout elements file is not a field: ask the instance for the real
|
|
645
|
+
// fields only, then read the child rows through the Table API.
|
|
646
|
+
const layoutRequests = (0, layoutElements_1.splitLayoutElementRequests)(missingFiles);
|
|
647
|
+
// `fingerprint` and `layoutElements` are client-only: stripped from the
|
|
648
|
+
// request, applied to the response (see fingerprint.ts, layoutElements.ts).
|
|
615
649
|
return client
|
|
616
650
|
.post(endpoint, {
|
|
617
|
-
missingFiles,
|
|
651
|
+
missingFiles: layoutRequests.request,
|
|
618
652
|
tableOptions: (0, fingerprint_1.stripClientOnlyTableOptions)(tableOptions),
|
|
619
653
|
})
|
|
620
|
-
.then(function (resp) {
|
|
654
|
+
.then(async function (resp) {
|
|
621
655
|
if (resp && resp.data && resp.data.result) {
|
|
622
656
|
(0, fingerprint_1.applyFingerprints)(resp.data.result, tableOptions);
|
|
657
|
+
await (0, layoutElements_1.fillLayoutElementContent)(resp.data.result, layoutRequests.wanted, readTableRows);
|
|
623
658
|
}
|
|
624
659
|
return resp;
|
|
625
660
|
});
|
|
@@ -639,12 +674,23 @@ const snClient = (baseURL, username, password, apiKey) => {
|
|
|
639
674
|
withFiles,
|
|
640
675
|
getContents: withFiles, // ServiceNow expects getContents, not withFiles
|
|
641
676
|
})
|
|
642
|
-
.then(function (resp) {
|
|
677
|
+
.then(async function (resp) {
|
|
643
678
|
// Fingerprinted fields come back as `<field>.sha256` entries (and with
|
|
644
679
|
// hashed content when withFiles is set), so the manifest, the
|
|
645
680
|
// missing-file check and every writer agree on the on-disk name.
|
|
646
681
|
if (resp && resp.data && resp.data.result && resp.data.result.tables) {
|
|
647
|
-
|
|
682
|
+
const tables = resp.data.result.tables;
|
|
683
|
+
(0, fingerprint_1.checkFingerprintOptions)(tableOptions, warnTableOption);
|
|
684
|
+
(0, fingerprint_1.applyFingerprints)(tables, tableOptions);
|
|
685
|
+
// Layout elements: list the file on every record of an enabled layout
|
|
686
|
+
// table, and read its content now when contents were asked for.
|
|
687
|
+
const layoutTables = (0, layoutElements_1.layoutElementTablesFor)(tableOptions, warnTableOption);
|
|
688
|
+
if (layoutTables.length > 0) {
|
|
689
|
+
(0, layoutElements_1.addLayoutElementFiles)(tables, layoutTables);
|
|
690
|
+
if (withFiles) {
|
|
691
|
+
await (0, layoutElements_1.fillLayoutElementContent)(tables, (0, layoutElements_1.recordIdsByTable)(tables, layoutTables), readTableRows);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
648
694
|
}
|
|
649
695
|
return resp;
|
|
650
696
|
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// After `dove create` syncs the new record, it prints where the files landed.
|
|
3
|
+
// That path was built from the display name, but the sync writes the record
|
|
4
|
+
// under its manifest folder, which carries a suffix when another record of the
|
|
5
|
+
// table has the same name ignoring letter case (folderNaming.ts). The printed
|
|
6
|
+
// path then named the OTHER record's folder on a case-insensitive disk. It now
|
|
7
|
+
// reads the folder from the manifest the sync wrote, by sys_id.
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
var logMessages = [];
|
|
13
|
+
function pushLog(level) {
|
|
14
|
+
return jest.fn(function (msg) {
|
|
15
|
+
logMessages.push({ level: level, msg: String(msg) });
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
jest.mock("../Logger", function () {
|
|
19
|
+
return {
|
|
20
|
+
logger: {
|
|
21
|
+
setLogLevel: jest.fn(),
|
|
22
|
+
success: pushLog("success"),
|
|
23
|
+
info: pushLog("info"),
|
|
24
|
+
error: pushLog("error"),
|
|
25
|
+
warn: pushLog("warn"),
|
|
26
|
+
debug: jest.fn(),
|
|
27
|
+
getLogLevel: function () { return "info"; },
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
jest.mock("../FileLogger", function () {
|
|
32
|
+
return {
|
|
33
|
+
fileLogger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() },
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
jest.mock("../commands", function () {
|
|
37
|
+
return { setLogLevel: jest.fn() };
|
|
38
|
+
});
|
|
39
|
+
// The scope manifest as the sync leaves it on disk.
|
|
40
|
+
var mockWrittenManifest = undefined;
|
|
41
|
+
var mockManifestAfterSync = undefined;
|
|
42
|
+
jest.mock("../config", function () {
|
|
43
|
+
return {
|
|
44
|
+
getManifest: jest.fn(function () { return undefined; }),
|
|
45
|
+
isMultiScopeManifest: jest.fn(function () { return false; }),
|
|
46
|
+
getConfig: jest.fn(function () { return {}; }),
|
|
47
|
+
getSourcePathForScope: jest.fn(function () { return "/virtual/src/x_cadso_ux"; }),
|
|
48
|
+
loadScopeManifest: jest.fn(function () { return Promise.resolve(mockWrittenManifest); }),
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
var mockSyncManifest = jest.fn(function () {
|
|
52
|
+
// syncManifest writes the full scope manifest before it returns.
|
|
53
|
+
mockWrittenManifest = mockManifestAfterSync;
|
|
54
|
+
return Promise.resolve({ failedScopes: [], notWritten: [] });
|
|
55
|
+
});
|
|
56
|
+
jest.mock("../appUtils", function () {
|
|
57
|
+
return { syncManifest: mockSyncManifest };
|
|
58
|
+
});
|
|
59
|
+
jest.mock("../projectFiles", function () {
|
|
60
|
+
return {
|
|
61
|
+
getUpdateSetsConfigPath: jest.fn(function () {
|
|
62
|
+
return "/nonexistent/dove-create-local-path-test/.dove-update-sets.json";
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
jest.mock("inquirer", function () {
|
|
67
|
+
return { prompt: jest.fn() };
|
|
68
|
+
});
|
|
69
|
+
var mockCreateRecord = jest.fn();
|
|
70
|
+
jest.mock("../snClient", function () {
|
|
71
|
+
return {
|
|
72
|
+
defaultClient: jest.fn(function () { return { createRecord: mockCreateRecord }; }),
|
|
73
|
+
unwrapSNResponse: jest.fn(),
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
const path_1 = __importDefault(require("path"));
|
|
77
|
+
const createRecordCommand_1 = require("../createRecordCommand");
|
|
78
|
+
var TABLE = "sys_ux_screen";
|
|
79
|
+
var SCOPE = "x_cadso_ux";
|
|
80
|
+
var NEW_ID = "fb55e60d00000000000000000000000a";
|
|
81
|
+
var OLD_ID = "0a1b2c3d00000000000000000000000b";
|
|
82
|
+
var SCOPE_DIR = "/virtual/src/x_cadso_ux";
|
|
83
|
+
function manifestWith(records) {
|
|
84
|
+
var entries = {};
|
|
85
|
+
var keys = Object.keys(records);
|
|
86
|
+
for (var i = 0; i < keys.length; i++) {
|
|
87
|
+
entries[keys[i]] = { name: keys[i], sys_id: records[keys[i]], files: [] };
|
|
88
|
+
}
|
|
89
|
+
var tables = {};
|
|
90
|
+
tables[TABLE] = { records: entries };
|
|
91
|
+
return { scope: SCOPE, tables: tables };
|
|
92
|
+
}
|
|
93
|
+
function locationMessages() {
|
|
94
|
+
return logMessages
|
|
95
|
+
.filter(function (m) { return m.msg.indexOf("Local files created at: ") !== -1; })
|
|
96
|
+
.map(function (m) { return m.msg; });
|
|
97
|
+
}
|
|
98
|
+
describe("dove create prints the folder the sync wrote", function () {
|
|
99
|
+
var exitSpy;
|
|
100
|
+
beforeEach(function () {
|
|
101
|
+
logMessages = [];
|
|
102
|
+
mockWrittenManifest = undefined;
|
|
103
|
+
mockManifestAfterSync = undefined;
|
|
104
|
+
jest.clearAllMocks();
|
|
105
|
+
exitSpy = jest.spyOn(process, "exit").mockImplementation(function (code) {
|
|
106
|
+
throw new Error("process.exit(" + code + ")");
|
|
107
|
+
});
|
|
108
|
+
mockCreateRecord.mockResolvedValue({
|
|
109
|
+
status: 200,
|
|
110
|
+
data: { result: { sys_id: NEW_ID, table: TABLE, name: "Search Default" } },
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
afterEach(function () {
|
|
114
|
+
exitSpy.mockRestore();
|
|
115
|
+
});
|
|
116
|
+
async function create() {
|
|
117
|
+
await (0, createRecordCommand_1.createRecordCommand)({ table: TABLE, name: "Search Default", scope: SCOPE, ci: true, logLevel: "info" });
|
|
118
|
+
}
|
|
119
|
+
it("names the suffixed folder of a letter-case twin, not the display name", async function () {
|
|
120
|
+
mockManifestAfterSync = manifestWith({
|
|
121
|
+
"Search default (0a1b2c3d)": OLD_ID,
|
|
122
|
+
"Search Default (fb55e60d)": NEW_ID,
|
|
123
|
+
});
|
|
124
|
+
await create();
|
|
125
|
+
expect(mockSyncManifest).toHaveBeenCalledWith(SCOPE, { record: { table: TABLE, sysId: NEW_ID } });
|
|
126
|
+
var located = locationMessages();
|
|
127
|
+
expect(located.length).toBe(1);
|
|
128
|
+
expect(located[0]).toContain(path_1.default.join(SCOPE_DIR, TABLE, "Search Default (fb55e60d)"));
|
|
129
|
+
expect(located[0].slice(-("Search Default".length + 1))).not.toBe(path_1.default.sep + "Search Default");
|
|
130
|
+
expect(exitSpy).not.toHaveBeenCalled();
|
|
131
|
+
});
|
|
132
|
+
it("names the plain folder when no other record shares the name", async function () {
|
|
133
|
+
mockManifestAfterSync = manifestWith({ "Search Default": NEW_ID });
|
|
134
|
+
await create();
|
|
135
|
+
var located = locationMessages();
|
|
136
|
+
expect(located.length).toBe(1);
|
|
137
|
+
expect(located[0]).toContain(path_1.default.join(SCOPE_DIR, TABLE, "Search Default"));
|
|
138
|
+
});
|
|
139
|
+
it("prints no location when the synced manifest does not list the new record", async function () {
|
|
140
|
+
mockManifestAfterSync = manifestWith({ "Search default": OLD_ID });
|
|
141
|
+
await create();
|
|
142
|
+
expect(locationMessages()).toEqual([]);
|
|
143
|
+
expect(exitSpy).not.toHaveBeenCalled();
|
|
144
|
+
});
|
|
145
|
+
});
|
|
@@ -4,6 +4,15 @@
|
|
|
4
4
|
// records live under their scope's folder (src/Core/..., or nested as
|
|
5
5
|
// src/UIComponents/<scope>/...), so the cleanup silently found nothing. It now
|
|
6
6
|
// uses the scope's own folder.
|
|
7
|
+
//
|
|
8
|
+
// The folder is the record's manifest key, found by sys_id. It used to be built
|
|
9
|
+
// from the instance's display name, which (a) is another record's folder when a
|
|
10
|
+
// local namesake exists and the deleted record was never refreshed, (b) is not
|
|
11
|
+
// the folder of any record the naming rule suffixed, and (c) is the scope
|
|
12
|
+
// folder itself for a display name of "..".
|
|
13
|
+
//
|
|
14
|
+
// Every assertion is on the real filesystem in a temp dir: the folders and the
|
|
15
|
+
// scope manifest file are real, only the instance call is stubbed.
|
|
7
16
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
18
|
};
|
|
@@ -29,19 +38,39 @@ jest.mock("../commands", function () {
|
|
|
29
38
|
var mockRoot = "";
|
|
30
39
|
jest.mock("../config", function () {
|
|
31
40
|
var p = jest.requireActual("path");
|
|
41
|
+
var realFs = jest.requireActual("fs");
|
|
42
|
+
var manifestPath = function (scope) {
|
|
43
|
+
return p.join(mockRoot, "dove.manifest." + scope + ".json");
|
|
44
|
+
};
|
|
32
45
|
return {
|
|
33
46
|
getSourcePath: function () { return p.join(mockRoot, "src"); },
|
|
34
47
|
getSourcePathForScope: function (scope) {
|
|
35
48
|
return p.join(mockRoot, "src", "UIComponents", scope);
|
|
36
49
|
},
|
|
37
|
-
|
|
50
|
+
getScopeManifestPath: manifestPath,
|
|
51
|
+
loadScopeManifest: function (scope) {
|
|
52
|
+
try {
|
|
53
|
+
return Promise.resolve(JSON.parse(realFs.readFileSync(manifestPath(scope), "utf8")));
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
return Promise.resolve(undefined);
|
|
57
|
+
}
|
|
58
|
+
},
|
|
38
59
|
getManifest: function () { return undefined; },
|
|
60
|
+
resolveManifestForScope: function () { return undefined; },
|
|
39
61
|
getConfig: function () { return { scopes: {} }; },
|
|
40
62
|
isMultiScopeManifest: function () { return false; },
|
|
41
63
|
};
|
|
42
64
|
});
|
|
43
65
|
jest.mock("../FileUtils", function () {
|
|
44
|
-
|
|
66
|
+
var p = jest.requireActual("path");
|
|
67
|
+
var realFs = jest.requireActual("fs");
|
|
68
|
+
return {
|
|
69
|
+
writeScopeManifest: function (scope, man) {
|
|
70
|
+
realFs.writeFileSync(p.join(mockRoot, "dove.manifest." + scope + ".json"), JSON.stringify(man, null, 2));
|
|
71
|
+
return Promise.resolve();
|
|
72
|
+
},
|
|
73
|
+
};
|
|
45
74
|
});
|
|
46
75
|
var mockDelete = jest.fn();
|
|
47
76
|
jest.mock("../snClient", function () {
|
|
@@ -54,42 +83,142 @@ jest.mock("../snClient", function () {
|
|
|
54
83
|
const fs_1 = __importDefault(require("fs"));
|
|
55
84
|
const os_1 = __importDefault(require("os"));
|
|
56
85
|
const path_1 = __importDefault(require("path"));
|
|
86
|
+
const Logger_1 = require("../Logger");
|
|
57
87
|
const deleteRecordCommand_1 = require("../deleteRecordCommand");
|
|
88
|
+
var SCOPE = "x_cadso_ui_panel";
|
|
89
|
+
var TABLE = "sys_ux_lib_component";
|
|
90
|
+
var SYS_A = "aaaaaaaa1111111111111111111111a1";
|
|
91
|
+
var SYS_B = "bbbbbbbb2222222222222222222222b2";
|
|
92
|
+
function scopeDir() {
|
|
93
|
+
return path_1.default.join(mockRoot, "src", "UIComponents", SCOPE);
|
|
94
|
+
}
|
|
95
|
+
function recordDir(folder) {
|
|
96
|
+
return path_1.default.join(scopeDir(), TABLE, folder);
|
|
97
|
+
}
|
|
98
|
+
function makeRecordFolder(folder) {
|
|
99
|
+
var dir = recordDir(folder);
|
|
100
|
+
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
101
|
+
fs_1.default.writeFileSync(path_1.default.join(dir, "metaData.json"), "{}");
|
|
102
|
+
return dir;
|
|
103
|
+
}
|
|
104
|
+
function writeManifest(records) {
|
|
105
|
+
var entries = {};
|
|
106
|
+
var keys = Object.keys(records);
|
|
107
|
+
for (var i = 0; i < keys.length; i++) {
|
|
108
|
+
entries[keys[i]] = { name: keys[i], sys_id: records[keys[i]], files: [{ name: "metaData", type: "json" }] };
|
|
109
|
+
}
|
|
110
|
+
var manifest = { scope: SCOPE, tables: {} };
|
|
111
|
+
manifest.tables[TABLE] = { records: entries };
|
|
112
|
+
fs_1.default.writeFileSync(path_1.default.join(mockRoot, "dove.manifest." + SCOPE + ".json"), JSON.stringify(manifest, null, 2));
|
|
113
|
+
}
|
|
114
|
+
function manifestKeys() {
|
|
115
|
+
var man = JSON.parse(fs_1.default.readFileSync(path_1.default.join(mockRoot, "dove.manifest." + SCOPE + ".json"), "utf8"));
|
|
116
|
+
if (!man.tables[TABLE])
|
|
117
|
+
return [];
|
|
118
|
+
return Object.keys(man.tables[TABLE].records).sort();
|
|
119
|
+
}
|
|
120
|
+
function warnings() {
|
|
121
|
+
return Logger_1.logger.warn.mock.calls
|
|
122
|
+
.map(function (c) { return String(c[0]); })
|
|
123
|
+
.join("\n");
|
|
124
|
+
}
|
|
125
|
+
function instanceNames(displayName) {
|
|
126
|
+
mockDelete.mockResolvedValue({ data: { result: { name: displayName } } });
|
|
127
|
+
}
|
|
58
128
|
describe("deleteRecordCommand local cleanup", function () {
|
|
59
129
|
beforeEach(function () {
|
|
60
130
|
mockRoot = fs_1.default.mkdtempSync(path_1.default.join(os_1.default.tmpdir(), "dove-delete-"));
|
|
61
131
|
mockDelete.mockReset();
|
|
62
|
-
|
|
132
|
+
Logger_1.logger.warn.mockClear();
|
|
133
|
+
instanceNames("Panel");
|
|
63
134
|
});
|
|
64
135
|
afterEach(function () {
|
|
65
136
|
fs_1.default.rmSync(mockRoot, { recursive: true, force: true });
|
|
66
137
|
});
|
|
67
138
|
it("removes the record folder under the scope's own folder", async function () {
|
|
68
|
-
var
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
await (0, deleteRecordCommand_1.deleteRecordCommand)({
|
|
72
|
-
table: "sys_ux_lib_component",
|
|
73
|
-
name: "Panel",
|
|
74
|
-
scope: "x_cadso_ui_panel",
|
|
75
|
-
sysid: "abc",
|
|
76
|
-
ci: true,
|
|
77
|
-
logLevel: "info",
|
|
78
|
-
});
|
|
139
|
+
var dir = makeRecordFolder("Panel");
|
|
140
|
+
writeManifest({ Panel: SYS_A });
|
|
141
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, name: "Panel", scope: SCOPE, sysid: SYS_A, ci: true, logLevel: "info" });
|
|
79
142
|
expect(mockDelete).toHaveBeenCalledTimes(1);
|
|
80
|
-
expect(fs_1.default.existsSync(
|
|
143
|
+
expect(fs_1.default.existsSync(dir)).toBe(false);
|
|
144
|
+
expect(manifestKeys()).toEqual([]);
|
|
81
145
|
});
|
|
82
146
|
it("leaves a same-named folder under the top-level source path alone", async function () {
|
|
83
|
-
var wrongDir = path_1.default.join(mockRoot, "src",
|
|
147
|
+
var wrongDir = path_1.default.join(mockRoot, "src", TABLE, "Panel");
|
|
84
148
|
fs_1.default.mkdirSync(wrongDir, { recursive: true });
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
name: "Panel",
|
|
88
|
-
scope: "x_cadso_ui_panel",
|
|
89
|
-
sysid: "abc",
|
|
90
|
-
ci: true,
|
|
91
|
-
logLevel: "info",
|
|
92
|
-
});
|
|
149
|
+
writeManifest({ Panel: SYS_A });
|
|
150
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, name: "Panel", scope: SCOPE, sysid: SYS_A, ci: true, logLevel: "info" });
|
|
93
151
|
expect(fs_1.default.existsSync(wrongDir)).toBe(true);
|
|
94
152
|
});
|
|
153
|
+
it("--sysid only: never touches a local namesake when the deleted record was never refreshed", async function () {
|
|
154
|
+
// Local record A is "Foo". B, also "Foo" on the instance, was created there
|
|
155
|
+
// and never refreshed, so the manifest does not know B.
|
|
156
|
+
var aDir = makeRecordFolder("Foo");
|
|
157
|
+
fs_1.default.writeFileSync(path_1.default.join(aDir, "script.js"), "// uncommitted edit");
|
|
158
|
+
writeManifest({ Foo: SYS_A });
|
|
159
|
+
instanceNames("Foo");
|
|
160
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, scope: SCOPE, sysid: SYS_B, ci: true, logLevel: "info" });
|
|
161
|
+
expect(mockDelete).toHaveBeenCalledTimes(1);
|
|
162
|
+
expect(fs_1.default.readFileSync(path_1.default.join(aDir, "script.js"), "utf8")).toBe("// uncommitted edit");
|
|
163
|
+
expect(manifestKeys()).toEqual(["Foo"]);
|
|
164
|
+
expect(warnings()).toContain("is not in the " + SCOPE + " manifest");
|
|
165
|
+
});
|
|
166
|
+
it("--sysid only: removes the suffixed folder the manifest gave the record, and only that one", async function () {
|
|
167
|
+
// Namesakes get " (first 8 of sys_id)" (folderNaming.ts). The display name
|
|
168
|
+
// "Foo" is neither record's folder.
|
|
169
|
+
var aDir = makeRecordFolder("Foo (aaaaaaaa)");
|
|
170
|
+
var bDir = makeRecordFolder("Foo (bbbbbbbb)");
|
|
171
|
+
writeManifest({ "Foo (aaaaaaaa)": SYS_A, "Foo (bbbbbbbb)": SYS_B });
|
|
172
|
+
instanceNames("Foo");
|
|
173
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, scope: SCOPE, sysid: SYS_B, ci: true, logLevel: "info" });
|
|
174
|
+
expect(fs_1.default.existsSync(bDir)).toBe(false);
|
|
175
|
+
expect(fs_1.default.existsSync(aDir)).toBe(true);
|
|
176
|
+
expect(manifestKeys()).toEqual(["Foo (aaaaaaaa)"]);
|
|
177
|
+
});
|
|
178
|
+
it("with a name and --sysid that disagree, the sys_id decides which folder goes", async function () {
|
|
179
|
+
var aDir = makeRecordFolder("Panel");
|
|
180
|
+
var bDir = makeRecordFolder("Panel (bbbbbbbb)");
|
|
181
|
+
writeManifest({ Panel: SYS_A, "Panel (bbbbbbbb)": SYS_B });
|
|
182
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, name: "Panel", scope: SCOPE, sysid: SYS_B, ci: true, logLevel: "info" });
|
|
183
|
+
expect(fs_1.default.existsSync(aDir)).toBe(true);
|
|
184
|
+
expect(fs_1.default.existsSync(bDir)).toBe(false);
|
|
185
|
+
expect(manifestKeys()).toEqual(["Panel"]);
|
|
186
|
+
});
|
|
187
|
+
it("--sysid only: a display name of '..' cannot point the delete at the scope folder", async function () {
|
|
188
|
+
// ".." is no safe folder name, so the manifest names the record by sys_id.
|
|
189
|
+
var otherDir = makeRecordFolder("Other");
|
|
190
|
+
var ownDir = makeRecordFolder(SYS_B);
|
|
191
|
+
var records = { Other: SYS_A };
|
|
192
|
+
records[SYS_B] = SYS_B;
|
|
193
|
+
writeManifest(records);
|
|
194
|
+
instanceNames("..");
|
|
195
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, scope: SCOPE, sysid: SYS_B, ci: true, logLevel: "info" });
|
|
196
|
+
expect(fs_1.default.existsSync(scopeDir())).toBe(true);
|
|
197
|
+
expect(fs_1.default.existsSync(otherDir)).toBe(true);
|
|
198
|
+
expect(fs_1.default.existsSync(ownDir)).toBe(false);
|
|
199
|
+
expect(manifestKeys()).toEqual(["Other"]);
|
|
200
|
+
});
|
|
201
|
+
it("refuses a manifest key that is not a direct child of <scope dir>/<table>", async function () {
|
|
202
|
+
// A hand-edited manifest: the key ".." would resolve to the scope folder.
|
|
203
|
+
var otherDir = makeRecordFolder("Other");
|
|
204
|
+
writeManifest({ Other: SYS_A, "..": SYS_B });
|
|
205
|
+
instanceNames("..");
|
|
206
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, scope: SCOPE, sysid: SYS_B, ci: true, logLevel: "info" });
|
|
207
|
+
expect(fs_1.default.existsSync(path_1.default.join(scopeDir(), TABLE))).toBe(true);
|
|
208
|
+
expect(fs_1.default.existsSync(otherDir)).toBe(true);
|
|
209
|
+
expect(warnings()).toContain("Not removing '..'");
|
|
210
|
+
});
|
|
211
|
+
it("leaves local files alone when the scope has no manifest at all", async function () {
|
|
212
|
+
var dir = makeRecordFolder("Panel");
|
|
213
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, name: "Panel", scope: SCOPE, sysid: SYS_A, ci: true, logLevel: "info" });
|
|
214
|
+
expect(fs_1.default.existsSync(dir)).toBe(true);
|
|
215
|
+
expect(warnings()).toContain("no local folder was removed");
|
|
216
|
+
});
|
|
217
|
+
it("--keep-local leaves the folder and the manifest as they are", async function () {
|
|
218
|
+
var dir = makeRecordFolder("Panel");
|
|
219
|
+
writeManifest({ Panel: SYS_A });
|
|
220
|
+
await (0, deleteRecordCommand_1.deleteRecordCommand)({ table: TABLE, scope: SCOPE, sysid: SYS_A, ci: true, keepLocal: true, logLevel: "info" });
|
|
221
|
+
expect(fs_1.default.existsSync(dir)).toBe(true);
|
|
222
|
+
expect(manifestKeys()).toEqual(["Panel"]);
|
|
223
|
+
});
|
|
95
224
|
});
|
|
@@ -126,6 +126,47 @@ describe("getFileContextWithSkipReason", function () {
|
|
|
126
126
|
expect(result.context).toBeUndefined();
|
|
127
127
|
expect(result.skipReason).toBe("fingerprint only, read-only");
|
|
128
128
|
});
|
|
129
|
+
it("refuses a content file whose field the manifest lists as a fingerprint", function () {
|
|
130
|
+
// script.js left from before `fingerprint` was set: refresh never deletes
|
|
131
|
+
// it, and the manifest field `script` matches it by name.
|
|
132
|
+
mockManifest = {
|
|
133
|
+
scope: "x_cadso_ui_form",
|
|
134
|
+
tables: {
|
|
135
|
+
sys_ux_lib_source_script: {
|
|
136
|
+
records: {
|
|
137
|
+
Bundle: {
|
|
138
|
+
sys_id: "f9cd",
|
|
139
|
+
files: [{ name: "script", type: "sha256" }, { name: "metaData", type: "json" }],
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
mockIsMultiScope = false;
|
|
146
|
+
var result = (0, FileUtils_1.getFileContextWithSkipReason)("/project/src/UIComponents/x_cadso_ui_form/sys_ux_lib_source_script/Bundle/script.js");
|
|
147
|
+
expect(result.context).toBeUndefined();
|
|
148
|
+
expect(result.skipReason).toBe("fingerprinted field, read-only");
|
|
149
|
+
});
|
|
150
|
+
it("still pushes the other fields of a record that has a fingerprinted field", function () {
|
|
151
|
+
mockManifest = {
|
|
152
|
+
scope: "x_cadso_ui_form",
|
|
153
|
+
tables: {
|
|
154
|
+
sys_ux_lib_source_script: {
|
|
155
|
+
records: {
|
|
156
|
+
Bundle: {
|
|
157
|
+
sys_id: "f9cd",
|
|
158
|
+
files: [{ name: "script", type: "sha256" }, { name: "source_map", type: "js" }],
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
mockIsMultiScope = false;
|
|
165
|
+
var result = (0, FileUtils_1.getFileContextWithSkipReason)("/project/src/UIComponents/x_cadso_ui_form/sys_ux_lib_source_script/Bundle/source_map.js");
|
|
166
|
+
expect(result.skipReason).toBeUndefined();
|
|
167
|
+
expect(result.context).toBeDefined();
|
|
168
|
+
expect(result.context.targetField).toBe("source_map");
|
|
169
|
+
});
|
|
129
170
|
it("throws when no manifest is loaded", function () {
|
|
130
171
|
mockManifest = null;
|
|
131
172
|
expect(function () {
|
|
@@ -56,6 +56,43 @@ describe("fingerprintFieldsFor", function () {
|
|
|
56
56
|
expect((0, fingerprint_1.fingerprintFieldsFor)(bad, "t2")).toEqual(["script"]);
|
|
57
57
|
});
|
|
58
58
|
});
|
|
59
|
+
describe("checkFingerprintOptions", function () {
|
|
60
|
+
function warningsFor(opts) {
|
|
61
|
+
const warnings = [];
|
|
62
|
+
(0, fingerprint_1.checkFingerprintOptions)(opts, function (m) { warnings.push(m); });
|
|
63
|
+
return warnings;
|
|
64
|
+
}
|
|
65
|
+
it("says nothing for a well-formed option, an empty array, or no option", function () {
|
|
66
|
+
expect(warningsFor(OPTIONS)).toEqual([]);
|
|
67
|
+
expect(warningsFor({ t: { fingerprint: [] } })).toEqual([]);
|
|
68
|
+
expect(warningsFor({ t: { fingerprint: undefined, query: "active=true" } })).toEqual([]);
|
|
69
|
+
expect(warningsFor(undefined)).toEqual([]);
|
|
70
|
+
expect(warningsFor(null)).toEqual([]);
|
|
71
|
+
});
|
|
72
|
+
it("warns when the option is not an array, so the bundle would be mirrored in full", function () {
|
|
73
|
+
const warnings = warningsFor({ sys_ux_lib_source_script: { fingerprint: "script" } });
|
|
74
|
+
expect(warnings.length).toBe(1);
|
|
75
|
+
expect(warnings[0]).toContain("tableOptions.sys_ux_lib_source_script.fingerprint is ignored");
|
|
76
|
+
expect(warnings[0]).toContain("array of field names");
|
|
77
|
+
});
|
|
78
|
+
it("names each entry that is not a field name", function () {
|
|
79
|
+
const warnings = warningsFor({ t2: { fingerprint: ["", " ", 5, " script", "script", null] } });
|
|
80
|
+
expect(warnings.length).toBe(1);
|
|
81
|
+
expect(warnings[0]).toContain("tableOptions.t2.fingerprint ignores");
|
|
82
|
+
expect(warnings[0]).toContain('"", " ", 5, " script", null');
|
|
83
|
+
expect(warnings[0]).not.toContain('"script",');
|
|
84
|
+
});
|
|
85
|
+
it("reports every malformed table, in table order", function () {
|
|
86
|
+
const warnings = warningsFor({
|
|
87
|
+
zz: { fingerprint: { script: true } },
|
|
88
|
+
aa: { fingerprint: 7 },
|
|
89
|
+
ok: { fingerprint: ["script"] },
|
|
90
|
+
});
|
|
91
|
+
expect(warnings.length).toBe(2);
|
|
92
|
+
expect(warnings[0]).toContain("tableOptions.aa.");
|
|
93
|
+
expect(warnings[1]).toContain("tableOptions.zz.");
|
|
94
|
+
});
|
|
95
|
+
});
|
|
59
96
|
describe("stripClientOnlyTableOptions", function () {
|
|
60
97
|
it("removes fingerprint and keeps every other option", function () {
|
|
61
98
|
var out = (0, fingerprint_1.stripClientOnlyTableOptions)(OPTIONS);
|