@featurevisor/core 3.0.1 → 3.2.0

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.
Files changed (208) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/tester/testFeature.js +17 -16
  13. package/lib/tester/testFeature.js.map +1 -1
  14. package/package.json +10 -7
  15. package/CHANGELOG.md +0 -2238
  16. package/coverage/clover.xml +0 -5456
  17. package/coverage/coverage-final.json +0 -64
  18. package/coverage/lcov-report/base.css +0 -224
  19. package/coverage/lcov-report/block-navigation.js +0 -87
  20. package/coverage/lcov-report/favicon.png +0 -0
  21. package/coverage/lcov-report/index.html +0 -371
  22. package/coverage/lcov-report/prettify.css +0 -1
  23. package/coverage/lcov-report/prettify.js +0 -2
  24. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  25. package/coverage/lcov-report/sorter.js +0 -210
  26. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  27. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  28. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  29. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  30. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  31. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  32. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  33. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  34. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  35. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  36. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  37. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  38. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  39. package/coverage/lcov-report/src/builder/index.html +0 -311
  40. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  41. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  42. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  43. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  44. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  45. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  46. package/coverage/lcov-report/src/cli/index.html +0 -131
  47. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  48. package/coverage/lcov-report/src/config/index.html +0 -131
  49. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  50. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  51. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  52. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  53. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  54. package/coverage/lcov-report/src/datasource/index.html +0 -161
  55. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  56. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  57. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  58. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  59. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  60. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  61. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  62. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  63. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  64. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  65. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  66. package/coverage/lcov-report/src/index.html +0 -131
  67. package/coverage/lcov-report/src/info/index.html +0 -116
  68. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  69. package/coverage/lcov-report/src/init/index.html +0 -116
  70. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  71. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  72. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  73. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  74. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  75. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  76. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  77. package/coverage/lcov-report/src/linter/index.html +0 -326
  78. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  79. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  80. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  81. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  82. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  83. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  84. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  85. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  86. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  87. package/coverage/lcov-report/src/list/index.html +0 -131
  88. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  89. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  90. package/coverage/lcov-report/src/promoter/index.html +0 -116
  91. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  92. package/coverage/lcov-report/src/sets.ts.html +0 -313
  93. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  94. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  95. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  96. package/coverage/lcov-report/src/tester/index.html +0 -206
  97. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  98. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  99. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  100. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  101. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  102. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  103. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  104. package/coverage/lcov-report/src/utils/index.html +0 -161
  105. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  106. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  107. package/coverage/lcov.info +0 -11615
  108. package/jest.config.js +0 -22
  109. package/json-schema/README.md +0 -5
  110. package/json-schema/attribute.json +0 -40
  111. package/json-schema/feature.json +0 -394
  112. package/json-schema/segment.json +0 -137
  113. package/src/assess-distribution/index.ts +0 -223
  114. package/src/benchmark/index.ts +0 -236
  115. package/src/builder/allocator.spec.ts +0 -105
  116. package/src/builder/allocator.ts +0 -45
  117. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  118. package/src/builder/applyContextToConditions.ts +0 -139
  119. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  120. package/src/builder/applyContextToDatafile.ts +0 -229
  121. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  122. package/src/builder/applyContextToSegments.ts +0 -165
  123. package/src/builder/buildDatafile.spec.ts +0 -445
  124. package/src/builder/buildDatafile.ts +0 -656
  125. package/src/builder/buildProject.ts +0 -344
  126. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  127. package/src/builder/getFeatureRanges.ts +0 -61
  128. package/src/builder/hashes.ts +0 -109
  129. package/src/builder/index.ts +0 -2
  130. package/src/builder/mutateVariables.spec.ts +0 -1179
  131. package/src/builder/mutateVariables.ts +0 -112
  132. package/src/builder/mutator.spec.ts +0 -413
  133. package/src/builder/mutator.ts +0 -237
  134. package/src/builder/revision.spec.ts +0 -22
  135. package/src/builder/revision.ts +0 -22
  136. package/src/builder/traffic.spec.ts +0 -946
  137. package/src/builder/traffic.ts +0 -192
  138. package/src/cli/cli.ts +0 -105
  139. package/src/cli/index.ts +0 -2
  140. package/src/cli/plugins.ts +0 -43
  141. package/src/config/index.ts +0 -1
  142. package/src/config/projectConfig.spec.ts +0 -194
  143. package/src/config/projectConfig.ts +0 -280
  144. package/src/datasource/adapter.ts +0 -56
  145. package/src/datasource/datasource.ts +0 -305
  146. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  147. package/src/datasource/filesystemAdapter.ts +0 -497
  148. package/src/datasource/index.ts +0 -3
  149. package/src/dependencies.ts +0 -13
  150. package/src/evaluate/index.ts +0 -243
  151. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  152. package/src/find-duplicate-segments/index.ts +0 -74
  153. package/src/find-usage/index.ts +0 -565
  154. package/src/generate-code/index.ts +0 -106
  155. package/src/generate-code/typescript.spec.ts +0 -214
  156. package/src/generate-code/typescript.ts +0 -608
  157. package/src/index.ts +0 -20
  158. package/src/info/index.ts +0 -129
  159. package/src/init/index.ts +0 -65
  160. package/src/linter/attributeSchema.spec.ts +0 -342
  161. package/src/linter/attributeSchema.ts +0 -626
  162. package/src/linter/checkCircularDependency.ts +0 -45
  163. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  164. package/src/linter/conditionSchema.spec.ts +0 -510
  165. package/src/linter/conditionSchema.ts +0 -713
  166. package/src/linter/featureSchema.spec.ts +0 -1985
  167. package/src/linter/featureSchema.ts +0 -1922
  168. package/src/linter/groupSchema.ts +0 -47
  169. package/src/linter/index.ts +0 -1
  170. package/src/linter/lintProject.spec.ts +0 -336
  171. package/src/linter/lintProject.ts +0 -873
  172. package/src/linter/mutationNotation.spec.ts +0 -704
  173. package/src/linter/mutationNotation.ts +0 -442
  174. package/src/linter/printError.ts +0 -68
  175. package/src/linter/schema.spec.ts +0 -690
  176. package/src/linter/schema.ts +0 -491
  177. package/src/linter/segmentSchema.spec.ts +0 -358
  178. package/src/linter/segmentSchema.ts +0 -18
  179. package/src/linter/targetSchema.spec.ts +0 -159
  180. package/src/linter/targetSchema.ts +0 -46
  181. package/src/linter/testSchema.spec.ts +0 -258
  182. package/src/linter/testSchema.ts +0 -148
  183. package/src/linter/zodHelpers.ts +0 -16
  184. package/src/list/index.spec.ts +0 -135
  185. package/src/list/index.ts +0 -660
  186. package/src/list/matrix.spec.ts +0 -49
  187. package/src/list/matrix.ts +0 -212
  188. package/src/promoter/index.spec.ts +0 -617
  189. package/src/promoter/index.ts +0 -1266
  190. package/src/sets.ts +0 -76
  191. package/src/targeting.spec.ts +0 -74
  192. package/src/targeting.ts +0 -106
  193. package/src/tester/cliFormat.ts +0 -16
  194. package/src/tester/helpers.spec.ts +0 -149
  195. package/src/tester/helpers.ts +0 -76
  196. package/src/tester/index.ts +0 -2
  197. package/src/tester/prettyDuration.ts +0 -34
  198. package/src/tester/printTestResult.ts +0 -84
  199. package/src/tester/testFeature.ts +0 -367
  200. package/src/tester/testProject.spec.ts +0 -50
  201. package/src/tester/testProject.ts +0 -311
  202. package/src/tester/testSegment.ts +0 -77
  203. package/src/utils/extractKeys.ts +0 -136
  204. package/src/utils/git.ts +0 -158
  205. package/src/utils/index.ts +0 -3
  206. package/src/utils/pretty.ts +0 -12
  207. package/tsconfig.cjs.json +0 -13
  208. package/tsconfig.typecheck.json +0 -9
@@ -1,311 +0,0 @@
1
- import * as fs from "fs";
2
-
3
- import type { TestSegment, TestFeature, Test, DatafileContent } from "@featurevisor/types";
4
-
5
- import { testSegment } from "./testSegment";
6
- import { testFeature } from "./testFeature";
7
- import { CLI_FORMAT_BOLD, CLI_FORMAT_GREEN, CLI_FORMAT_RED } from "./cliFormat";
8
- import { Dependencies } from "../dependencies";
9
- import { prettyDuration } from "./prettyDuration";
10
- import { printTestResult } from "./printTestResult";
11
-
12
- import { buildDatafile, buildTargetDatafile } from "../builder";
13
- import { Plugin } from "../cli";
14
- import { listEntities } from "../list";
15
- import { getProjectSetExecutions, printSetHeader } from "../sets";
16
- import { resolveTargets } from "../targeting";
17
-
18
- export interface TestProjectOptions {
19
- keyPattern?: string;
20
- assertionPattern?: string;
21
- verbose?: boolean;
22
- showDatafile?: boolean;
23
- onlyFailures?: boolean;
24
- inflate?: number;
25
- target?: string | string[];
26
- }
27
-
28
- export interface ExecutionResult {
29
- passed: boolean;
30
- assertionsCount: {
31
- passed: number;
32
- failed: number;
33
- };
34
- }
35
-
36
- // the key can be either "<EnvironmentKey>" or "<EnvironmentKey>-target-<Target>"
37
- export type DatafileContentByKey = Map<string | false, DatafileContent>;
38
-
39
- export function filterTestForTargets(test: Test, selectedTargetKeys?: string[]): Test | undefined {
40
- if (!selectedTargetKeys || !(test as TestFeature).feature) return test;
41
-
42
- const featureTest = test as TestFeature;
43
- const assertions = featureTest.assertions.filter(
44
- (assertion) => !assertion.target || selectedTargetKeys.includes(assertion.target),
45
- );
46
-
47
- return assertions.length > 0 ? { ...featureTest, assertions } : undefined;
48
- }
49
-
50
- async function buildTestDatafilesForEnvironment(
51
- deps: Dependencies,
52
- datafileContentByKey: DatafileContentByKey,
53
- environment: string | false,
54
- selectedTargetKeys?: string[],
55
- ) {
56
- const { projectConfig, datasource, options } = deps;
57
- const existingState = await datasource.readState(environment);
58
- const datafileContent = await buildDatafile(
59
- projectConfig,
60
- datasource,
61
- {
62
- revision: "include-all-features",
63
- environment,
64
- inflate: options.inflate,
65
- },
66
- existingState,
67
- );
68
-
69
- datafileContentByKey.set(environment, datafileContent as DatafileContent);
70
-
71
- const targetKeys = selectedTargetKeys || (await datasource.listTargets());
72
- for (const targetKey of targetKeys) {
73
- const target = await datasource.readTarget(targetKey);
74
- const targetDatafileContent = await buildTargetDatafile({
75
- projectConfig,
76
- datasource,
77
- target,
78
- environment,
79
- existingState,
80
- revision: "include-target-features",
81
- inflate: options.inflate,
82
- });
83
-
84
- datafileContentByKey.set(`${environment}-target-${targetKey}`, targetDatafileContent);
85
- }
86
- }
87
-
88
- export async function executeTest(
89
- test: Test,
90
- deps: Dependencies,
91
- options: TestProjectOptions,
92
- datafileContentByKey: DatafileContentByKey,
93
- ): Promise<ExecutionResult | undefined> {
94
- const { datasource, projectConfig, rootDirectoryPath } = deps;
95
-
96
- const extension = datasource.getExtension();
97
- const relativeTestFilePath = test.key + (extension ? `.${extension}` : "");
98
-
99
- const tAsSegment = test as TestSegment;
100
- const tAsFeature = test as TestFeature;
101
- const key = tAsSegment.segment || tAsFeature.feature;
102
- const type = tAsSegment.segment ? "segment" : "feature";
103
-
104
- const executionResult: ExecutionResult = {
105
- passed: true,
106
- assertionsCount: {
107
- passed: 0,
108
- failed: 0,
109
- },
110
- };
111
-
112
- if (!key) {
113
- console.error(` => Invalid test: ${JSON.stringify(test)}`);
114
- executionResult.passed = false;
115
-
116
- return executionResult;
117
- }
118
-
119
- let testResult;
120
- if (type === "segment") {
121
- testResult = await testSegment(datasource, tAsSegment, options);
122
- } else {
123
- testResult = await testFeature(
124
- datasource,
125
- projectConfig,
126
- tAsFeature,
127
- options,
128
- datafileContentByKey,
129
- );
130
- }
131
-
132
- if (!options.onlyFailures) {
133
- // show all
134
- printTestResult(testResult, relativeTestFilePath, rootDirectoryPath);
135
- } else {
136
- // show failed only
137
- if (!testResult.passed) {
138
- printTestResult(testResult, relativeTestFilePath, rootDirectoryPath);
139
- }
140
- }
141
-
142
- if (!testResult.passed) {
143
- executionResult.passed = false;
144
-
145
- executionResult.assertionsCount.failed = testResult.assertions.filter((a) => !a.passed).length;
146
- executionResult.assertionsCount.passed +=
147
- testResult.assertions.length - executionResult.assertionsCount.failed;
148
- } else {
149
- executionResult.assertionsCount.passed = testResult.assertions.length;
150
- }
151
-
152
- return executionResult;
153
- }
154
-
155
- export async function testProject(
156
- deps: Dependencies,
157
- testOptions: TestProjectOptions = {},
158
- ): Promise<boolean> {
159
- const { rootDirectoryPath, projectConfig, datasource, options } = deps;
160
-
161
- let hasError = false;
162
-
163
- if (!fs.existsSync(projectConfig.testsDirectoryPath)) {
164
- console.error(`Tests directory does not exist: ${projectConfig.testsDirectoryPath}`);
165
- hasError = true;
166
-
167
- return hasError;
168
- }
169
-
170
- const startTime = Date.now();
171
-
172
- let passedTestsCount = 0;
173
- let failedTestsCount = 0;
174
-
175
- let passedAssertionsCount = 0;
176
- let failedAssertionsCount = 0;
177
-
178
- const datafileContentByKey: DatafileContentByKey = new Map();
179
- const selectedTargets = testOptions.target
180
- ? await resolveTargets(datasource, testOptions.target, { defaultToAll: false })
181
- : undefined;
182
- const selectedTargetKeys = selectedTargets?.map((target) => target.key);
183
-
184
- // with environments
185
- if (Array.isArray(projectConfig.environments)) {
186
- for (const environment of projectConfig.environments) {
187
- await buildTestDatafilesForEnvironment(
188
- deps,
189
- datafileContentByKey,
190
- environment,
191
- selectedTargetKeys,
192
- );
193
- }
194
- }
195
-
196
- // no environments
197
- if (!Array.isArray(projectConfig.environments)) {
198
- await buildTestDatafilesForEnvironment(deps, datafileContentByKey, false, selectedTargetKeys);
199
- }
200
-
201
- const tests = await listEntities<Test>(
202
- {
203
- rootDirectoryPath,
204
- projectConfig,
205
- datasource,
206
- options: {
207
- ...testOptions,
208
- applyMatrix: true,
209
- },
210
- },
211
- "test",
212
- );
213
-
214
- for (const originalTest of tests) {
215
- const test = filterTestForTargets(originalTest, selectedTargetKeys);
216
- if (!test) continue;
217
-
218
- const executionResult = await executeTest(test, deps, options, datafileContentByKey);
219
-
220
- if (!executionResult) {
221
- continue;
222
- }
223
-
224
- if (executionResult.passed) {
225
- passedTestsCount += 1;
226
- } else {
227
- hasError = true;
228
- failedTestsCount += 1;
229
- }
230
-
231
- passedAssertionsCount += executionResult.assertionsCount.passed;
232
- failedAssertionsCount += executionResult.assertionsCount.failed;
233
- }
234
-
235
- const diffInMs = Date.now() - startTime;
236
-
237
- if (options.onlyFailures !== true || hasError) {
238
- console.log("\n---");
239
- }
240
- console.log("");
241
-
242
- const testSpecsMessage = `Test specs: ${passedTestsCount} passed, ${failedTestsCount} failed`;
243
- const testAssertionsMessage = `Assertions: ${passedAssertionsCount} passed, ${failedAssertionsCount} failed`;
244
- if (hasError) {
245
- console.log(CLI_FORMAT_RED, testSpecsMessage);
246
- console.log(CLI_FORMAT_RED, testAssertionsMessage);
247
- } else {
248
- console.log(CLI_FORMAT_GREEN, testSpecsMessage);
249
- console.log(CLI_FORMAT_GREEN, testAssertionsMessage);
250
- }
251
-
252
- console.log(CLI_FORMAT_BOLD, `Time: ${prettyDuration(diffInMs)}`);
253
-
254
- return hasError;
255
- }
256
-
257
- export const testPlugin: Plugin = {
258
- command: "test",
259
- handler: async function ({ rootDirectoryPath, projectConfig, datasource, parsed }) {
260
- const executions = await getProjectSetExecutions(projectConfig, datasource, parsed.set);
261
- let hasError = false;
262
-
263
- for (const execution of executions) {
264
- printSetHeader(projectConfig, execution.set);
265
-
266
- const executionHasError = await testProject(
267
- {
268
- rootDirectoryPath,
269
- projectConfig: execution.projectConfig,
270
- datasource: execution.datasource,
271
- options: parsed,
272
- },
273
- parsed as TestProjectOptions,
274
- );
275
-
276
- if (executionHasError) {
277
- hasError = true;
278
- }
279
- }
280
-
281
- if (hasError) {
282
- return false;
283
- }
284
- },
285
- examples: [
286
- {
287
- command: "test",
288
- description: "run all tests",
289
- },
290
- {
291
- command: "test --keyPattern=pattern",
292
- description: "run tests matching key pattern",
293
- },
294
- {
295
- command: "test --assertionPattern=pattern",
296
- description: "run tests matching assertion pattern",
297
- },
298
- {
299
- command: "test --onlyFailures",
300
- description: "run only failed tests",
301
- },
302
- {
303
- command: "test --showDatafile",
304
- description: "show datafile content for each test",
305
- },
306
- {
307
- command: "test --verbose",
308
- description: "show all test results",
309
- },
310
- ],
311
- };
@@ -1,77 +0,0 @@
1
- import type {
2
- TestSegment,
3
- Condition,
4
- TestResult,
5
- TestResultAssertion,
6
- TestResultAssertionError,
7
- } from "@featurevisor/types";
8
- import { allConditionsAreMatched } from "@featurevisor/sdk";
9
-
10
- import { Datasource } from "../datasource";
11
-
12
- export async function testSegment(
13
- datasource: Datasource,
14
- test: TestSegment,
15
- _options: { verbose?: boolean; quiet?: boolean } = {},
16
- ): Promise<TestResult> {
17
- void _options;
18
-
19
- const testStartTime = Date.now();
20
- const segmentKey = test.segment;
21
-
22
- const testResult: TestResult = {
23
- type: "segment",
24
- key: segmentKey,
25
-
26
- // to be updated later
27
- notFound: false,
28
- duration: 0,
29
- passed: true,
30
- assertions: [],
31
- };
32
-
33
- const segmentExists = await datasource.segmentExists(segmentKey);
34
-
35
- if (!segmentExists) {
36
- testResult.notFound = true;
37
- testResult.passed = false;
38
-
39
- return testResult;
40
- }
41
-
42
- const parsedSegment = await datasource.readSegment(segmentKey);
43
- const conditions = parsedSegment.conditions as Condition | Condition[];
44
-
45
- test.assertions.forEach(function (assertion) {
46
- const assertionStartTime = Date.now();
47
- const testResultAssertion: TestResultAssertion = {
48
- description: assertion.description as string,
49
- duration: 0,
50
- passed: true,
51
- errors: [],
52
- };
53
-
54
- const expected = assertion.expectedToMatch;
55
- const actual = allConditionsAreMatched(conditions, assertion.context);
56
- const passed = actual === expected;
57
-
58
- if (!passed) {
59
- const testResultAssertionError: TestResultAssertionError = {
60
- type: "segment",
61
- expected,
62
- actual,
63
- };
64
-
65
- (testResultAssertion.errors as TestResultAssertionError[]).push(testResultAssertionError);
66
- testResult.passed = false;
67
- testResultAssertion.passed = passed;
68
- }
69
-
70
- testResult.assertions.push(testResultAssertion);
71
- testResultAssertion.duration = Date.now() - assertionStartTime;
72
- });
73
-
74
- testResult.duration = Date.now() - testStartTime;
75
-
76
- return testResult;
77
- }
@@ -1,136 +0,0 @@
1
- import type {
2
- Condition,
3
- AttributeKey,
4
- GroupSegment,
5
- SegmentKey,
6
- Feature,
7
- } from "@featurevisor/types";
8
-
9
- export function extractSegmentsFromFeature(feature: Feature): Set<SegmentKey> {
10
- const result = new Set<SegmentKey>();
11
-
12
- // rules
13
- for (const traffic of feature.traffic) {
14
- if (traffic.segments) {
15
- extractSegmentKeysFromGroupSegments(traffic.segments).forEach((segmentKey) => {
16
- result.add(segmentKey);
17
- });
18
- }
19
- }
20
-
21
- // force
22
- if (feature.force) {
23
- for (const f of feature.force) {
24
- if (f.segments) {
25
- extractSegmentKeysFromGroupSegments(f.segments).forEach((segmentKey) => {
26
- result.add(segmentKey);
27
- });
28
- }
29
- }
30
- }
31
-
32
- // variable overrides from inside variations
33
- if (feature.variations) {
34
- for (const variation of feature.variations) {
35
- if (variation.variableOverrides) {
36
- for (const variableKey in variation.variableOverrides) {
37
- const overrides = variation.variableOverrides[variableKey];
38
-
39
- if (overrides) {
40
- for (const override of overrides) {
41
- if (override.segments) {
42
- extractSegmentKeysFromGroupSegments(override.segments).forEach((segmentKey) => {
43
- result.add(segmentKey);
44
- });
45
- }
46
- }
47
- }
48
- }
49
- }
50
- }
51
- }
52
-
53
- return result;
54
- }
55
-
56
- export function extractSegmentKeysFromGroupSegments(
57
- segments: GroupSegment | GroupSegment[],
58
- ): Set<SegmentKey> {
59
- const result = new Set<SegmentKey>();
60
-
61
- if (Array.isArray(segments)) {
62
- segments.forEach((segment) => {
63
- extractSegmentKeysFromGroupSegments(segment).forEach((segmentKey) => {
64
- result.add(segmentKey);
65
- });
66
- });
67
- }
68
-
69
- if (typeof segments === "object") {
70
- if ("and" in segments) {
71
- extractSegmentKeysFromGroupSegments(segments.and).forEach((segmentKey) => {
72
- result.add(segmentKey);
73
- });
74
- }
75
-
76
- if ("or" in segments) {
77
- extractSegmentKeysFromGroupSegments(segments.or).forEach((segmentKey) => {
78
- result.add(segmentKey);
79
- });
80
- }
81
-
82
- if ("not" in segments) {
83
- extractSegmentKeysFromGroupSegments(segments.not).forEach((segmentKey) => {
84
- result.add(segmentKey);
85
- });
86
- }
87
- }
88
-
89
- if (typeof segments === "string") {
90
- result.add(segments);
91
- }
92
-
93
- return result;
94
- }
95
-
96
- export function extractAttributeKeysFromConditions(
97
- conditions: Condition | Condition[],
98
- ): Set<AttributeKey> {
99
- const result = new Set<AttributeKey>();
100
-
101
- if (Array.isArray(conditions)) {
102
- conditions.forEach((condition) => {
103
- extractAttributeKeysFromConditions(condition).forEach((attributeKey) => {
104
- result.add(attributeKey);
105
- });
106
- });
107
- }
108
-
109
- if (typeof conditions === "string") {
110
- return result;
111
- }
112
-
113
- if ("attribute" in conditions) {
114
- result.add(conditions.attribute);
115
- }
116
-
117
- if ("and" in conditions) {
118
- extractAttributeKeysFromConditions(conditions.and).forEach((attributeKey) => {
119
- result.add(attributeKey);
120
- });
121
- }
122
-
123
- if ("or" in conditions) {
124
- extractAttributeKeysFromConditions(conditions.or).forEach((attributeKey) => {
125
- result.add(attributeKey);
126
- });
127
- }
128
-
129
- if ("not" in conditions) {
130
- extractAttributeKeysFromConditions(conditions.not).forEach((attributeKey) => {
131
- result.add(attributeKey);
132
- });
133
- }
134
-
135
- return result;
136
- }
package/src/utils/git.ts DELETED
@@ -1,158 +0,0 @@
1
- import * as path from "path";
2
-
3
- import type { Commit, EntityDiff, EntityType } from "@featurevisor/types";
4
- import { CustomParser } from "@featurevisor/parsers";
5
-
6
- import { ProjectConfig } from "../config";
7
-
8
- function isWithinDirectory(directoryPath: string, fileDirectoryPath: string): boolean {
9
- return (
10
- fileDirectoryPath === directoryPath || fileDirectoryPath.startsWith(directoryPath + path.sep)
11
- );
12
- }
13
-
14
- function parseGitCommitShowOutput(gitShowOutput: string) {
15
- const result = {
16
- hash: "",
17
- author: "",
18
- date: "",
19
- message: "",
20
- diffs: {},
21
- };
22
-
23
- const lines = gitShowOutput.split("\n");
24
- let currentFile = "";
25
- let parsingDiffs = false;
26
- let parsingMessage = false;
27
-
28
- lines.forEach((line) => {
29
- if (line.startsWith("commit ")) {
30
- result.hash = line.replace("commit ", "").trim();
31
- } else if (line.startsWith("Author:")) {
32
- result.author = line.replace("Author:", "").trim();
33
- parsingMessage = false;
34
- } else if (line.startsWith("Date:")) {
35
- result.date = line.replace("Date:", "").trim();
36
- parsingMessage = true; // Start parsing commit message
37
- } else if (line.startsWith("diff --git")) {
38
- if (currentFile) {
39
- // Finish capturing the previous file's diff
40
- parsingDiffs = false;
41
- }
42
- currentFile = line.split(" ")[2].substring(2);
43
- result.diffs[currentFile] = line + "\n"; // Include the diff --git line
44
- parsingDiffs = true;
45
- parsingMessage = false; // Stop parsing commit message
46
- } else if (parsingDiffs) {
47
- result.diffs[currentFile] += line + "\n";
48
- } else if (parsingMessage && line.trim() !== "") {
49
- result.message += line.trim() + "\n"; // Capture commit message
50
- }
51
- });
52
-
53
- return result;
54
- }
55
-
56
- function analyzeFileChange(diff) {
57
- let status = "updated"; // Default to 'updated'
58
-
59
- // Check for file creation or deletion
60
- if (diff.includes("new file mode")) {
61
- status = "created";
62
- } else if (diff.includes("deleted file mode")) {
63
- status = "deleted";
64
- }
65
-
66
- return status;
67
- }
68
-
69
- export function getCommit(
70
- gitShowOutput: string,
71
- options: { rootDirectoryPath: string; projectConfig: ProjectConfig },
72
- ): Commit {
73
- const parsed = parseGitCommitShowOutput(gitShowOutput);
74
- const { rootDirectoryPath, projectConfig } = options;
75
-
76
- const commit: Commit = {
77
- hash: parsed.hash,
78
- author: parsed.author,
79
- timestamp: parsed.date,
80
- entities: [],
81
- };
82
-
83
- Object.keys(parsed.diffs).forEach((file) => {
84
- const diff = parsed.diffs[file];
85
- const status = analyzeFileChange(diff);
86
-
87
- const absolutePath = path.join(rootDirectoryPath, file);
88
- const relativeDir = path.dirname(absolutePath);
89
-
90
- // get entity type
91
- let type: EntityType = "attribute";
92
- if (isWithinDirectory(projectConfig.attributesDirectoryPath, relativeDir)) {
93
- type = "attribute";
94
- } else if (isWithinDirectory(projectConfig.segmentsDirectoryPath, relativeDir)) {
95
- type = "segment";
96
- } else if (isWithinDirectory(projectConfig.featuresDirectoryPath, relativeDir)) {
97
- type = "feature";
98
- } else if (isWithinDirectory(projectConfig.groupsDirectoryPath, relativeDir)) {
99
- type = "group";
100
- } else if (isWithinDirectory(projectConfig.schemasDirectoryPath, relativeDir)) {
101
- type = "schema";
102
- } else if (isWithinDirectory(projectConfig.targetsDirectoryPath, relativeDir)) {
103
- type = "target";
104
- } else if (isWithinDirectory(projectConfig.testsDirectoryPath, relativeDir)) {
105
- type = "test";
106
- } else {
107
- // unknown type
108
- return;
109
- }
110
-
111
- // get entity key
112
- const fileName = absolutePath.split(path.sep).pop() as string;
113
- const extensionWithDot = "." + (projectConfig.parser as CustomParser).extension;
114
-
115
- if (!fileName.endsWith(extensionWithDot)) {
116
- // unknown extension
117
- return;
118
- }
119
-
120
- let key = fileName.replace(extensionWithDot, "");
121
-
122
- if (
123
- (type === "feature" || type === "target") &&
124
- absolutePath.startsWith(
125
- (type === "feature"
126
- ? projectConfig.featuresDirectoryPath
127
- : projectConfig.targetsDirectoryPath) + path.sep,
128
- )
129
- ) {
130
- const entityDirectoryPath =
131
- type === "feature"
132
- ? projectConfig.featuresDirectoryPath
133
- : projectConfig.targetsDirectoryPath;
134
- key = absolutePath
135
- .replace(entityDirectoryPath + path.sep, "")
136
- .replace(extensionWithDot, "")
137
- .replace(/\\/g, "/");
138
- }
139
-
140
- const entityDiff: EntityDiff = {
141
- type,
142
- key,
143
- content: diff,
144
- };
145
-
146
- if (status === "created") {
147
- entityDiff.created = true;
148
- } else if (status === "deleted") {
149
- entityDiff.deleted = true;
150
- } else {
151
- entityDiff.updated = true;
152
- }
153
-
154
- commit.entities.push(entityDiff);
155
- });
156
-
157
- return commit;
158
- }
@@ -1,3 +0,0 @@
1
- export * from "./extractKeys";
2
- export * from "./git";
3
- export * from "./pretty";
@@ -1,12 +0,0 @@
1
- export function prettyNumber(n: number) {
2
- return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
3
- }
4
-
5
- export function prettyPercentage(
6
- value: number,
7
- total: number,
8
- precision: number = 2,
9
- suffix: string = "%",
10
- ): string {
11
- return `${((value / total) * 100).toFixed(precision)}${suffix}`;
12
- }
package/tsconfig.cjs.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.cjs.json",
3
- "compilerOptions": {
4
- "target": "es2020",
5
- "lib": ["es2020"],
6
- "types": ["node"],
7
- "outDir": "./lib",
8
- "rootDir": "./src",
9
- "skipLibCheck": true
10
- },
11
- "include": ["./src/**/*.ts"],
12
- "exclude": ["./src/**/*.spec.ts"]
13
- }