More feature flag renaming
This commit is contained in:
parent
701cea34ba
commit
43c3ed9c28
11 changed files with 105 additions and 102 deletions
2
lib/codeql.test.js
generated
2
lib/codeql.test.js
generated
|
|
@ -195,7 +195,7 @@ const TOOLCACHE_BYPASS_TEST_CASES = [
|
|||
],
|
||||
];
|
||||
for (const [isFeatureEnabled, toolsInput, shouldToolcacheBeBypassed,] of TOOLCACHE_BYPASS_TEST_CASES) {
|
||||
(0, ava_1.default)(`download codeql bundle ${shouldToolcacheBeBypassed ? "bypasses" : "does not bypass"} toolcache when feature flag ${isFeatureEnabled ? "enabled" : "disabled"} and tools: ${toolsInput} passed`, async (t) => {
|
||||
(0, ava_1.default)(`download codeql bundle ${shouldToolcacheBeBypassed ? "bypasses" : "does not bypass"} toolcache when feature ${isFeatureEnabled ? "enabled" : "disabled"} and tools: ${toolsInput} passed`, async (t) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
|
||||
await mockApiAndSetupCodeQL({
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
12
lib/config-utils.test.js
generated
12
lib/config-utils.test.js
generated
|
|
@ -968,7 +968,7 @@ parseInputAndConfigErrorMacro.title = (providedTitle) => `Parse Packs input and
|
|||
(0, ava_1.default)("input with + only", parseInputAndConfigErrorMacro, {}, " + ", [languages_1.Language.cpp], true, /remove the '\+'/);
|
||||
(0, ava_1.default)("input with invalid pack name", parseInputAndConfigErrorMacro, {}, " xxx", [languages_1.Language.cpp], false, /"xxx" is not a valid pack/);
|
||||
const mlPoweredQueriesMacro = ava_1.default.macro({
|
||||
exec: async (t, codeQLVersion, isMlPoweredQueriesFlagEnabled, packsInput, queriesInput, expectedVersionString) => {
|
||||
exec: async (t, codeQLVersion, isMlPoweredQueriesEnabled, packsInput, queriesInput, expectedVersionString) => {
|
||||
return await util.withTmpDir(async (tmpDir) => {
|
||||
const codeQL = (0, codeql_1.setCodeQL)({
|
||||
async getVersion() {
|
||||
|
|
@ -987,7 +987,7 @@ const mlPoweredQueriesMacro = ava_1.default.macro({
|
|||
return { packs: [] };
|
||||
},
|
||||
});
|
||||
const { packs } = await configUtils.initConfig("javascript", queriesInput, packsInput, undefined, undefined, undefined, false, false, "", "", { owner: "github", repo: "example " }, tmpDir, codeQL, tmpDir, gitHubVersion, sampleApiDetails, (0, testing_utils_1.createFeatures)(isMlPoweredQueriesFlagEnabled ? [feature_flags_1.Feature.MlPoweredQueriesEnabled] : []), (0, logging_1.getRunnerLogger)(true));
|
||||
const { packs } = await configUtils.initConfig("javascript", queriesInput, packsInput, undefined, undefined, undefined, false, false, "", "", { owner: "github", repo: "example " }, tmpDir, codeQL, tmpDir, gitHubVersion, sampleApiDetails, (0, testing_utils_1.createFeatures)(isMlPoweredQueriesEnabled ? [feature_flags_1.Feature.MlPoweredQueriesEnabled] : []), (0, logging_1.getRunnerLogger)(true));
|
||||
if (expectedVersionString !== undefined) {
|
||||
t.deepEqual(packs, {
|
||||
[languages_1.Language.javascript]: [
|
||||
|
|
@ -1000,12 +1000,12 @@ const mlPoweredQueriesMacro = ava_1.default.macro({
|
|||
}
|
||||
});
|
||||
},
|
||||
title: (_providedTitle, codeQLVersion, isMlPoweredQueriesFlagEnabled, packsInput, queriesInput, expectedVersionString) => `ML-powered queries ${expectedVersionString !== undefined
|
||||
title: (_providedTitle, codeQLVersion, isMlPoweredQueriesEnabled, packsInput, queriesInput, expectedVersionString) => `ML-powered queries ${expectedVersionString !== undefined
|
||||
? `${expectedVersionString} are`
|
||||
: "aren't"} loaded for packs: ${packsInput}, queries: ${queriesInput} using CLI v${codeQLVersion} when feature flag is ${isMlPoweredQueriesFlagEnabled ? "enabled" : "disabled"}`,
|
||||
: "aren't"} loaded for packs: ${packsInput}, queries: ${queriesInput} using CLI v${codeQLVersion} when feature is ${isMlPoweredQueriesEnabled ? "enabled" : "disabled"}`,
|
||||
});
|
||||
// macro, codeQLVersion, isMlPoweredQueriesFlagEnabled, packsInput, queriesInput, expectedVersionString
|
||||
// Test that ML-powered queries aren't run when the feature flag is off.
|
||||
// macro, codeQLVersion, isMlPoweredQueriesEnabled, packsInput, queriesInput, expectedVersionString
|
||||
// Test that ML-powered queries aren't run when the feature is off.
|
||||
(0, ava_1.default)(mlPoweredQueriesMacro, "2.7.5", false, undefined, "security-extended", undefined);
|
||||
// Test that the ~0.1.0 version of ML-powered queries is run on v2.8.3 of the CLI.
|
||||
(0, ava_1.default)(mlPoweredQueriesMacro, "2.8.3", true, undefined, "security-extended", process.platform === "win32" ? undefined : "~0.1.0");
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
14
lib/feature-flags.js
generated
14
lib/feature-flags.js
generated
|
|
@ -55,7 +55,7 @@ exports.featureConfig = {
|
|||
/**
|
||||
* Determines the enablement status of a number of features.
|
||||
* If feature enablement is not able to be determined locally, a request to the
|
||||
* github API is made to determine the enablement status.
|
||||
* GitHub API is made to determine the enablement status.
|
||||
*/
|
||||
class Features {
|
||||
constructor(gitHubVersion, apiDetails, repositoryNwo, logger) {
|
||||
|
|
@ -63,15 +63,15 @@ class Features {
|
|||
}
|
||||
/**
|
||||
*
|
||||
* @param feature The feature flag to check.
|
||||
* @param feature The feature to check.
|
||||
* @param codeql An optional CodeQL object. If provided, and a `minimumVersion` is specified for the
|
||||
* feature flag, the version of the CodeQL CLI will be checked against the minimum version.
|
||||
* If the version is less than the minimum version, the feature flag will be considered
|
||||
* disabled. If not provided, and a `minimumVersion` is specified for the feature flag, the
|
||||
* feature, the version of the CodeQL CLI will be checked against the minimum version.
|
||||
* If the version is less than the minimum version, the feature will be considered
|
||||
* disabled. If not provided, and a `minimumVersion` is specified for the feature, the
|
||||
* this function will throw.
|
||||
* @returns true if the feature flag is enabled, false otherwise.
|
||||
* @returns true if the feature is enabled, false otherwise.
|
||||
*
|
||||
* @throws if a `minimumVersion` is specified for the feature flag, and `codeql` is not provided.
|
||||
* @throws if a `minimumVersion` is specified for the feature, and `codeql` is not provided.
|
||||
*/
|
||||
async getValue(feature, codeql) {
|
||||
if (!codeql && exports.featureConfig[feature].minimumVersion) {
|
||||
|
|
|
|||
66
lib/feature-flags.test.js
generated
66
lib/feature-flags.test.js
generated
|
|
@ -19,20 +19,20 @@ const testApiDetails = {
|
|||
apiURL: undefined,
|
||||
};
|
||||
const testRepositoryNwo = (0, repository_1.parseRepositoryNwo)("github/example");
|
||||
const ALL_FEATURE_FLAGS_DISABLED_VARIANTS = [
|
||||
const ALL_FEATURES_DISABLED_VARIANTS = [
|
||||
{
|
||||
description: "GHES",
|
||||
gitHubVersion: { type: util_1.GitHubVariant.GHES, version: "3.0.0" },
|
||||
},
|
||||
{ description: "GHAE", gitHubVersion: { type: util_1.GitHubVariant.GHAE } },
|
||||
];
|
||||
for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
|
||||
(0, ava_1.default)(`All feature flags are disabled if running against ${variant.description}`, async (t) => {
|
||||
for (const variant of ALL_FEATURES_DISABLED_VARIANTS) {
|
||||
(0, ava_1.default)(`All features are disabled if running against ${variant.description}`, async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const loggedMessages = [];
|
||||
const featureEnablement = setUpTests(tmpDir, (0, testing_utils_1.getRecordingLogger)(loggedMessages), variant.gitHubVersion);
|
||||
for (const flag of Object.values(feature_flags_1.Feature)) {
|
||||
t.false(await featureEnablement.getValue(flag, includeCodeQlIfRequired(flag)));
|
||||
for (const feature of Object.values(feature_flags_1.Feature)) {
|
||||
t.false(await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature)));
|
||||
}
|
||||
t.assert(loggedMessages.find((v) => v.type === "debug" &&
|
||||
v.message ===
|
||||
|
|
@ -45,19 +45,19 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
|
|||
const loggedMessages = [];
|
||||
const featureEnablement = setUpTests(tmpDir, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
|
||||
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(403, {});
|
||||
for (const flag of Object.values(feature_flags_1.Feature)) {
|
||||
t.assert((await featureEnablement.getValue(flag, includeCodeQlIfRequired(flag))) === false);
|
||||
for (const feature of Object.values(feature_flags_1.Feature)) {
|
||||
t.assert((await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature))) === false);
|
||||
}
|
||||
assertAllFeaturesUndefinedInApi(t, loggedMessages);
|
||||
});
|
||||
});
|
||||
(0, ava_1.default)("Feature flags are disabled if they're not returned in API response", async (t) => {
|
||||
(0, ava_1.default)("Features are disabled if they're not returned in API response", async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const loggedMessages = [];
|
||||
const featureEnablement = setUpTests(tmpDir, (0, testing_utils_1.getRecordingLogger)(loggedMessages));
|
||||
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, {});
|
||||
for (const flag of Object.values(feature_flags_1.Feature)) {
|
||||
t.assert((await featureEnablement.getValue(flag, includeCodeQlIfRequired(flag))) === false);
|
||||
for (const feature of Object.values(feature_flags_1.Feature)) {
|
||||
t.assert((await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature))) === false);
|
||||
}
|
||||
assertAllFeaturesUndefinedInApi(t, loggedMessages);
|
||||
});
|
||||
|
|
@ -72,50 +72,50 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
|
|||
});
|
||||
});
|
||||
for (const feature of Object.keys(feature_flags_1.featureConfig)) {
|
||||
(0, ava_1.default)(`Only feature flag '${feature}' is enabled if enabled in the API response. Other flags disabled`, async (t) => {
|
||||
(0, ava_1.default)(`Only feature '${feature}' is enabled if enabled in the API response. Other features disabled`, async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
// set all feature flags to false except the one we're testing
|
||||
// set all features to false except the one we're testing
|
||||
const expectedFeatureEnablement = {};
|
||||
for (const f of Object.keys(feature_flags_1.featureConfig)) {
|
||||
expectedFeatureEnablement[f] = f === feature;
|
||||
}
|
||||
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureEnablement);
|
||||
// retrieve the values of the actual feature flags
|
||||
// retrieve the values of the actual features
|
||||
const actualFeatureEnablement = {};
|
||||
for (const f of Object.keys(feature_flags_1.featureConfig)) {
|
||||
actualFeatureEnablement[f] = await featureEnablement.getValue(f, includeCodeQlIfRequired(f));
|
||||
}
|
||||
// Alls flags should be false except the one we're testing
|
||||
// All features should be false except the one we're testing
|
||||
t.deepEqual(actualFeatureEnablement, expectedFeatureEnablement);
|
||||
});
|
||||
});
|
||||
(0, ava_1.default)(`Only feature flag '${feature}' is enabled if the associated environment variable is true. Others disabled.`, async (t) => {
|
||||
(0, ava_1.default)(`Only feature '${feature}' is enabled if the associated environment variable is true. Others disabled.`, async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
const expectedFeatureEnablement = initializeFeatures(false);
|
||||
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureEnablement);
|
||||
// feature flag should be disabled initially
|
||||
// feature should be disabled initially
|
||||
t.assert(!(await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature))));
|
||||
// set env var to true and check that the feature flag is now enabled
|
||||
// set env var to true and check that the feature is now enabled
|
||||
process.env[feature_flags_1.featureConfig[feature].envVar] = "true";
|
||||
t.assert(await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature)));
|
||||
});
|
||||
});
|
||||
(0, ava_1.default)(`Feature flag '${feature}' is disabled if the associated environment variable is false, even if enabled in API`, async (t) => {
|
||||
(0, ava_1.default)(`Feature '${feature}' is disabled if the associated environment variable is false, even if enabled in API`, async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
const expectedFeatureEnablement = initializeFeatures(true);
|
||||
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureEnablement);
|
||||
// feature flag should be enabled initially
|
||||
// feature should be enabled initially
|
||||
t.assert(await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature)));
|
||||
// set env var to false and check that the feature flag is now disabled
|
||||
// set env var to false and check that the feature is now disabled
|
||||
process.env[feature_flags_1.featureConfig[feature].envVar] = "false";
|
||||
t.assert(!(await featureEnablement.getValue(feature, includeCodeQlIfRequired(feature))));
|
||||
});
|
||||
});
|
||||
if (feature_flags_1.featureConfig[feature].minimumVersion !== undefined) {
|
||||
(0, ava_1.default)(`Getting Feature Flag '${feature} should throw if no codeql is provided`, async (t) => {
|
||||
(0, ava_1.default)(`Getting feature '${feature} should throw if no codeql is provided`, async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
const expectedFeatureEnablement = initializeFeatures(true);
|
||||
|
|
@ -127,39 +127,39 @@ for (const feature of Object.keys(feature_flags_1.featureConfig)) {
|
|||
});
|
||||
}
|
||||
if (feature_flags_1.featureConfig[feature].minimumVersion !== undefined) {
|
||||
(0, ava_1.default)(`Feature flag '${feature}' is disabled if the minimum CLI version is below ${feature_flags_1.featureConfig[feature].minimumVersion}`, async (t) => {
|
||||
(0, ava_1.default)(`Feature '${feature}' is disabled if the minimum CLI version is below ${feature_flags_1.featureConfig[feature].minimumVersion}`, async (t) => {
|
||||
await (0, util_1.withTmpDir)(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
const expectedFeatureEnablement = initializeFeatures(true);
|
||||
(0, testing_utils_1.mockFeatureFlagApiEndpoint)(200, expectedFeatureEnablement);
|
||||
// feature flag should be disabled when an old CLI version is set
|
||||
// feature should be disabled when an old CLI version is set
|
||||
let codeql = (0, testing_utils_1.mockCodeQLVersion)("2.0.0");
|
||||
t.assert(!(await featureEnablement.getValue(feature, codeql)));
|
||||
// even setting the env var to true should not enable the feature flag if
|
||||
// even setting the env var to true should not enable the feature if
|
||||
// the minimum CLI version is not met
|
||||
process.env[feature_flags_1.featureConfig[feature].envVar] = "true";
|
||||
t.assert(!(await featureEnablement.getValue(feature, codeql)));
|
||||
// feature flag should be enabled when a new CLI version is set
|
||||
// feature should be enabled when a new CLI version is set
|
||||
// and env var is not set
|
||||
process.env[feature_flags_1.featureConfig[feature].envVar] = "";
|
||||
codeql = (0, testing_utils_1.mockCodeQLVersion)(feature_flags_1.featureConfig[feature].minimumVersion);
|
||||
t.assert(await featureEnablement.getValue(feature, codeql));
|
||||
// set env var to false and check that the feature flag is now disabled
|
||||
// set env var to false and check that the feature is now disabled
|
||||
process.env[feature_flags_1.featureConfig[feature].envVar] = "false";
|
||||
t.assert(!(await featureEnablement.getValue(feature, codeql)));
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
// If we ever run into a situation where we no longer have any feature flags that
|
||||
// If we ever run into a situation where we no longer have any features that
|
||||
// specify a minimum version, then we will have a bunch of code no longer being
|
||||
// tested. This is unlikely, and this test will fail if that happens.
|
||||
// If we do end up in that situation, then we should consider adding a synthetic
|
||||
// feature flag with a minimum version that is only used for tests.
|
||||
// feature with a minimum version that is only used for tests.
|
||||
(0, ava_1.default)("At least one feature has a minimum version specified", (t) => {
|
||||
t.assert(Object.values(feature_flags_1.featureConfig).some((f) => f.minimumVersion !== undefined), "At least one feature flag should have a minimum version specified");
|
||||
// An even less likely scenario is that we no longer have any feature flags.
|
||||
t.assert(Object.values(feature_flags_1.featureConfig).length > 0, "There should be at least one feature flag");
|
||||
t.assert(Object.values(feature_flags_1.featureConfig).some((f) => f.minimumVersion !== undefined), "At least one feature should have a minimum version specified");
|
||||
// An even less likely scenario is that we no longer have any features.
|
||||
t.assert(Object.values(feature_flags_1.featureConfig).length > 0, "There should be at least one feature");
|
||||
});
|
||||
function assertAllFeaturesUndefinedInApi(t, loggedMessages) {
|
||||
for (const feature of Object.keys(feature_flags_1.featureConfig)) {
|
||||
|
|
@ -178,8 +178,8 @@ function setUpTests(tmpDir, logger = (0, logging_1.getRunnerLogger)(true), gitHu
|
|||
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
|
||||
return new feature_flags_1.Features(gitHubVersion, testApiDetails, testRepositoryNwo, logger);
|
||||
}
|
||||
function includeCodeQlIfRequired(featureFlag) {
|
||||
return feature_flags_1.featureConfig[featureFlag].minimumVersion !== undefined
|
||||
function includeCodeQlIfRequired(feature) {
|
||||
return feature_flags_1.featureConfig[feature].minimumVersion !== undefined
|
||||
? (0, testing_utils_1.mockCodeQLVersion)("9.9.9")
|
||||
: undefined;
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -262,7 +262,7 @@ for (const [
|
|||
] of TOOLCACHE_BYPASS_TEST_CASES) {
|
||||
test(`download codeql bundle ${
|
||||
shouldToolcacheBeBypassed ? "bypasses" : "does not bypass"
|
||||
} toolcache when feature flag ${
|
||||
} toolcache when feature ${
|
||||
isFeatureEnabled ? "enabled" : "disabled"
|
||||
} and tools: ${toolsInput} passed`, async (t) => {
|
||||
await util.withTmpDir(async (tmpDir) => {
|
||||
|
|
|
|||
|
|
@ -1894,7 +1894,7 @@ const mlPoweredQueriesMacro = test.macro({
|
|||
exec: async (
|
||||
t: ExecutionContext,
|
||||
codeQLVersion: string,
|
||||
isMlPoweredQueriesFlagEnabled: boolean,
|
||||
isMlPoweredQueriesEnabled: boolean,
|
||||
packsInput: string | undefined,
|
||||
queriesInput: string | undefined,
|
||||
expectedVersionString: string | undefined
|
||||
|
|
@ -1936,7 +1936,7 @@ const mlPoweredQueriesMacro = test.macro({
|
|||
gitHubVersion,
|
||||
sampleApiDetails,
|
||||
createFeatures(
|
||||
isMlPoweredQueriesFlagEnabled ? [Feature.MlPoweredQueriesEnabled] : []
|
||||
isMlPoweredQueriesEnabled ? [Feature.MlPoweredQueriesEnabled] : []
|
||||
),
|
||||
getRunnerLogger(true)
|
||||
);
|
||||
|
|
@ -1954,7 +1954,7 @@ const mlPoweredQueriesMacro = test.macro({
|
|||
title: (
|
||||
_providedTitle: string | undefined,
|
||||
codeQLVersion: string,
|
||||
isMlPoweredQueriesFlagEnabled: boolean,
|
||||
isMlPoweredQueriesEnabled: boolean,
|
||||
packsInput: string | undefined,
|
||||
queriesInput: string | undefined,
|
||||
expectedVersionString: string | undefined
|
||||
|
|
@ -1963,13 +1963,13 @@ const mlPoweredQueriesMacro = test.macro({
|
|||
expectedVersionString !== undefined
|
||||
? `${expectedVersionString} are`
|
||||
: "aren't"
|
||||
} loaded for packs: ${packsInput}, queries: ${queriesInput} using CLI v${codeQLVersion} when feature flag is ${
|
||||
isMlPoweredQueriesFlagEnabled ? "enabled" : "disabled"
|
||||
} loaded for packs: ${packsInput}, queries: ${queriesInput} using CLI v${codeQLVersion} when feature is ${
|
||||
isMlPoweredQueriesEnabled ? "enabled" : "disabled"
|
||||
}`,
|
||||
});
|
||||
|
||||
// macro, codeQLVersion, isMlPoweredQueriesFlagEnabled, packsInput, queriesInput, expectedVersionString
|
||||
// Test that ML-powered queries aren't run when the feature flag is off.
|
||||
// macro, codeQLVersion, isMlPoweredQueriesEnabled, packsInput, queriesInput, expectedVersionString
|
||||
// Test that ML-powered queries aren't run when the feature is off.
|
||||
test(
|
||||
mlPoweredQueriesMacro,
|
||||
"2.7.5",
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ const testApiDetails: GitHubApiDetails = {
|
|||
|
||||
const testRepositoryNwo = parseRepositoryNwo("github/example");
|
||||
|
||||
const ALL_FEATURE_FLAGS_DISABLED_VARIANTS: Array<{
|
||||
const ALL_FEATURES_DISABLED_VARIANTS: Array<{
|
||||
description: string;
|
||||
gitHubVersion: util.GitHubVersion;
|
||||
}> = [
|
||||
|
|
@ -45,8 +45,8 @@ const ALL_FEATURE_FLAGS_DISABLED_VARIANTS: Array<{
|
|||
{ description: "GHAE", gitHubVersion: { type: GitHubVariant.GHAE } },
|
||||
];
|
||||
|
||||
for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
|
||||
test(`All feature flags are disabled if running against ${variant.description}`, async (t) => {
|
||||
for (const variant of ALL_FEATURES_DISABLED_VARIANTS) {
|
||||
test(`All features are disabled if running against ${variant.description}`, async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const loggedMessages = [];
|
||||
const featureEnablement = setUpTests(
|
||||
|
|
@ -55,9 +55,12 @@ for (const variant of ALL_FEATURE_FLAGS_DISABLED_VARIANTS) {
|
|||
variant.gitHubVersion
|
||||
);
|
||||
|
||||
for (const flag of Object.values(Feature)) {
|
||||
for (const feature of Object.values(Feature)) {
|
||||
t.false(
|
||||
await featureEnablement.getValue(flag, includeCodeQlIfRequired(flag))
|
||||
await featureEnablement.getValue(
|
||||
feature,
|
||||
includeCodeQlIfRequired(feature)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -83,11 +86,11 @@ test("API response missing", async (t) => {
|
|||
|
||||
mockFeatureFlagApiEndpoint(403, {});
|
||||
|
||||
for (const flag of Object.values(Feature)) {
|
||||
for (const feature of Object.values(Feature)) {
|
||||
t.assert(
|
||||
(await featureEnablement.getValue(
|
||||
flag,
|
||||
includeCodeQlIfRequired(flag)
|
||||
feature,
|
||||
includeCodeQlIfRequired(feature)
|
||||
)) === false
|
||||
);
|
||||
}
|
||||
|
|
@ -95,7 +98,7 @@ test("API response missing", async (t) => {
|
|||
});
|
||||
});
|
||||
|
||||
test("Feature flags are disabled if they're not returned in API response", async (t) => {
|
||||
test("Features are disabled if they're not returned in API response", async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const loggedMessages: LoggedMessage[] = [];
|
||||
const featureEnablement = setUpTests(
|
||||
|
|
@ -105,11 +108,11 @@ test("Feature flags are disabled if they're not returned in API response", async
|
|||
|
||||
mockFeatureFlagApiEndpoint(200, {});
|
||||
|
||||
for (const flag of Object.values(Feature)) {
|
||||
for (const feature of Object.values(Feature)) {
|
||||
t.assert(
|
||||
(await featureEnablement.getValue(
|
||||
flag,
|
||||
includeCodeQlIfRequired(flag)
|
||||
feature,
|
||||
includeCodeQlIfRequired(feature)
|
||||
)) === false
|
||||
);
|
||||
}
|
||||
|
|
@ -139,19 +142,19 @@ test("Feature flags exception is propagated if the API request errors", async (t
|
|||
});
|
||||
|
||||
for (const feature of Object.keys(featureConfig)) {
|
||||
test(`Only feature flag '${feature}' is enabled if enabled in the API response. Other flags disabled`, async (t) => {
|
||||
test(`Only feature '${feature}' is enabled if enabled in the API response. Other features disabled`, async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
|
||||
// set all feature flags to false except the one we're testing
|
||||
const expectedFeatureEnablement: { [flag: string]: boolean } = {};
|
||||
// set all features to false except the one we're testing
|
||||
const expectedFeatureEnablement: { [feature: string]: boolean } = {};
|
||||
for (const f of Object.keys(featureConfig)) {
|
||||
expectedFeatureEnablement[f] = f === feature;
|
||||
}
|
||||
mockFeatureFlagApiEndpoint(200, expectedFeatureEnablement);
|
||||
|
||||
// retrieve the values of the actual feature flags
|
||||
const actualFeatureEnablement: { [flag: string]: boolean } = {};
|
||||
// retrieve the values of the actual features
|
||||
const actualFeatureEnablement: { [feature: string]: boolean } = {};
|
||||
for (const f of Object.keys(featureConfig)) {
|
||||
actualFeatureEnablement[f] = await featureEnablement.getValue(
|
||||
f as Feature,
|
||||
|
|
@ -159,19 +162,19 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
);
|
||||
}
|
||||
|
||||
// Alls flags should be false except the one we're testing
|
||||
// All features should be false except the one we're testing
|
||||
t.deepEqual(actualFeatureEnablement, expectedFeatureEnablement);
|
||||
});
|
||||
});
|
||||
|
||||
test(`Only feature flag '${feature}' is enabled if the associated environment variable is true. Others disabled.`, async (t) => {
|
||||
test(`Only feature '${feature}' is enabled if the associated environment variable is true. Others disabled.`, async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
|
||||
const expectedFeatureEnablement = initializeFeatures(false);
|
||||
mockFeatureFlagApiEndpoint(200, expectedFeatureEnablement);
|
||||
|
||||
// feature flag should be disabled initially
|
||||
// feature should be disabled initially
|
||||
t.assert(
|
||||
!(await featureEnablement.getValue(
|
||||
feature as Feature,
|
||||
|
|
@ -179,7 +182,7 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
))
|
||||
);
|
||||
|
||||
// set env var to true and check that the feature flag is now enabled
|
||||
// set env var to true and check that the feature is now enabled
|
||||
process.env[featureConfig[feature].envVar] = "true";
|
||||
t.assert(
|
||||
await featureEnablement.getValue(
|
||||
|
|
@ -190,14 +193,14 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
});
|
||||
});
|
||||
|
||||
test(`Feature flag '${feature}' is disabled if the associated environment variable is false, even if enabled in API`, async (t) => {
|
||||
test(`Feature '${feature}' is disabled if the associated environment variable is false, even if enabled in API`, async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
|
||||
const expectedFeatureEnablement = initializeFeatures(true);
|
||||
mockFeatureFlagApiEndpoint(200, expectedFeatureEnablement);
|
||||
|
||||
// feature flag should be enabled initially
|
||||
// feature should be enabled initially
|
||||
t.assert(
|
||||
await featureEnablement.getValue(
|
||||
feature as Feature,
|
||||
|
|
@ -205,7 +208,7 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
)
|
||||
);
|
||||
|
||||
// set env var to false and check that the feature flag is now disabled
|
||||
// set env var to false and check that the feature is now disabled
|
||||
process.env[featureConfig[feature].envVar] = "false";
|
||||
t.assert(
|
||||
!(await featureEnablement.getValue(
|
||||
|
|
@ -217,7 +220,7 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
});
|
||||
|
||||
if (featureConfig[feature].minimumVersion !== undefined) {
|
||||
test(`Getting Feature Flag '${feature} should throw if no codeql is provided`, async (t) => {
|
||||
test(`Getting feature '${feature} should throw if no codeql is provided`, async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
|
||||
|
|
@ -235,33 +238,33 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
}
|
||||
|
||||
if (featureConfig[feature].minimumVersion !== undefined) {
|
||||
test(`Feature flag '${feature}' is disabled if the minimum CLI version is below ${featureConfig[feature].minimumVersion}`, async (t) => {
|
||||
test(`Feature '${feature}' is disabled if the minimum CLI version is below ${featureConfig[feature].minimumVersion}`, async (t) => {
|
||||
await withTmpDir(async (tmpDir) => {
|
||||
const featureEnablement = setUpTests(tmpDir);
|
||||
|
||||
const expectedFeatureEnablement = initializeFeatures(true);
|
||||
mockFeatureFlagApiEndpoint(200, expectedFeatureEnablement);
|
||||
|
||||
// feature flag should be disabled when an old CLI version is set
|
||||
// feature should be disabled when an old CLI version is set
|
||||
let codeql = mockCodeQLVersion("2.0.0");
|
||||
t.assert(
|
||||
!(await featureEnablement.getValue(feature as Feature, codeql))
|
||||
);
|
||||
|
||||
// even setting the env var to true should not enable the feature flag if
|
||||
// even setting the env var to true should not enable the feature if
|
||||
// the minimum CLI version is not met
|
||||
process.env[featureConfig[feature].envVar] = "true";
|
||||
t.assert(
|
||||
!(await featureEnablement.getValue(feature as Feature, codeql))
|
||||
);
|
||||
|
||||
// feature flag should be enabled when a new CLI version is set
|
||||
// feature should be enabled when a new CLI version is set
|
||||
// and env var is not set
|
||||
process.env[featureConfig[feature].envVar] = "";
|
||||
codeql = mockCodeQLVersion(featureConfig[feature].minimumVersion);
|
||||
t.assert(await featureEnablement.getValue(feature as Feature, codeql));
|
||||
|
||||
// set env var to false and check that the feature flag is now disabled
|
||||
// set env var to false and check that the feature is now disabled
|
||||
process.env[featureConfig[feature].envVar] = "false";
|
||||
t.assert(
|
||||
!(await featureEnablement.getValue(feature as Feature, codeql))
|
||||
|
|
@ -271,21 +274,21 @@ for (const feature of Object.keys(featureConfig)) {
|
|||
}
|
||||
}
|
||||
|
||||
// If we ever run into a situation where we no longer have any feature flags that
|
||||
// If we ever run into a situation where we no longer have any features that
|
||||
// specify a minimum version, then we will have a bunch of code no longer being
|
||||
// tested. This is unlikely, and this test will fail if that happens.
|
||||
// If we do end up in that situation, then we should consider adding a synthetic
|
||||
// feature flag with a minimum version that is only used for tests.
|
||||
// feature with a minimum version that is only used for tests.
|
||||
test("At least one feature has a minimum version specified", (t) => {
|
||||
t.assert(
|
||||
Object.values(featureConfig).some((f) => f.minimumVersion !== undefined),
|
||||
"At least one feature flag should have a minimum version specified"
|
||||
"At least one feature should have a minimum version specified"
|
||||
);
|
||||
|
||||
// An even less likely scenario is that we no longer have any feature flags.
|
||||
// An even less likely scenario is that we no longer have any features.
|
||||
t.assert(
|
||||
Object.values(featureConfig).length > 0,
|
||||
"There should be at least one feature flag"
|
||||
"There should be at least one feature"
|
||||
);
|
||||
});
|
||||
|
||||
|
|
@ -319,8 +322,8 @@ function setUpTests(
|
|||
return new Features(gitHubVersion, testApiDetails, testRepositoryNwo, logger);
|
||||
}
|
||||
|
||||
function includeCodeQlIfRequired(featureFlag: string) {
|
||||
return featureConfig[featureFlag].minimumVersion !== undefined
|
||||
function includeCodeQlIfRequired(feature: string) {
|
||||
return featureConfig[feature].minimumVersion !== undefined
|
||||
? mockCodeQLVersion("9.9.9")
|
||||
: undefined;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ type GitHubFeatureFlagsApiResponse = Partial<Record<Feature, boolean>>;
|
|||
/**
|
||||
* Determines the enablement status of a number of features.
|
||||
* If feature enablement is not able to be determined locally, a request to the
|
||||
* github API is made to determine the enablement status.
|
||||
* GitHub API is made to determine the enablement status.
|
||||
*/
|
||||
export class Features implements FeatureEnablement {
|
||||
private gitHubFeatureFlags: GitHubFeatureFlags;
|
||||
|
|
@ -74,15 +74,15 @@ export class Features implements FeatureEnablement {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param feature The feature flag to check.
|
||||
* @param feature The feature to check.
|
||||
* @param codeql An optional CodeQL object. If provided, and a `minimumVersion` is specified for the
|
||||
* feature flag, the version of the CodeQL CLI will be checked against the minimum version.
|
||||
* If the version is less than the minimum version, the feature flag will be considered
|
||||
* disabled. If not provided, and a `minimumVersion` is specified for the feature flag, the
|
||||
* feature, the version of the CodeQL CLI will be checked against the minimum version.
|
||||
* If the version is less than the minimum version, the feature will be considered
|
||||
* disabled. If not provided, and a `minimumVersion` is specified for the feature, the
|
||||
* this function will throw.
|
||||
* @returns true if the feature flag is enabled, false otherwise.
|
||||
* @returns true if the feature is enabled, false otherwise.
|
||||
*
|
||||
* @throws if a `minimumVersion` is specified for the feature flag, and `codeql` is not provided.
|
||||
* @throws if a `minimumVersion` is specified for the feature, and `codeql` is not provided.
|
||||
*/
|
||||
async getValue(feature: Feature, codeql?: CodeQL): Promise<boolean> {
|
||||
if (!codeql && featureConfig[feature].minimumVersion) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue