Skip to content

Commit b972533

Browse files
committed
much work
1 parent 457bf58 commit b972533

File tree

9 files changed

+150
-18
lines changed

9 files changed

+150
-18
lines changed

.github/workflows/demo-run-codeql-unit-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Export unit test matrix
3030
id: export-unit-test-matrix
3131
run: |
32-
echo "matrix=$(cat supported_codeql_configs.json | jq --compact-output '.supported_environment | map([.+{os: "ubuntu-20.04-xl", codeql_standard_library_ident : .codeql_standard_library | sub("\/"; "_")}]) | flatten | {include: .}')" >> $GITHUB_OUTPUT
32+
echo "matrix=$(qlt test run get-matrix --os-version ubuntu-latest)" >> $GITHUB_OUTPUT
3333
3434
run-test-suites:
3535
name: Run Unit Tests

qlt.conf.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"CodeQLCLI": "2.11.6",
3+
"CodeQLStandardLibrary": "codeql-cli/v2.11.6",
4+
"CodeQLCLIBundle": "codeql-bundle-20221211"
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CodeQLToolkit.Features.Test.Commands
8+
{
9+
public abstract class BaseGetMatrixCommandTarget : CommandTarget
10+
{
11+
public string[] OSVersions { get; set; }
12+
}
13+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using CodeQLToolkit.Shared.Utils;
2+
using System.Text.Json;
3+
4+
namespace CodeQLToolkit.Features.Test.Commands.Targets.Actions
5+
{
6+
[AutomationType(AutomationType.ACTIONS)]
7+
public class GetMatrixCommandTarget : BaseGetMatrixCommandTarget
8+
{
9+
public override void Run()
10+
{
11+
12+
// based on the current configuration of the repository, generate a matrix
13+
// for actions, it looks like this:
14+
15+
// {
16+
// "include": [
17+
// {
18+
// "codeql_cli": "2.12.6",
19+
// "codeql_standard_library": "codeql-cli/v2.12.6",
20+
// "codeql_cli_bundle": "codeql-bundle-20230403",
21+
// "os": "ubuntu-latest",
22+
// "codeql_standard_library_ident": "codeql-cli_v2.12.6"
23+
// }
24+
// ]
25+
// }
26+
27+
// For now, we only support a single version but this is easy to extend. The options for runners are what create different matrix types.
28+
var c = new QLTConfig()
29+
{
30+
Base = Base
31+
};
32+
33+
if (!File.Exists(c.CodeQLConfigFilePath))
34+
{
35+
ProcessUtils.DieWithError($"Cannot read values from missing file {c.CodeQLConfigFilePath}");
36+
}
37+
38+
var config = c.FromFile();
39+
40+
List<object> configs = new List<object>();
41+
42+
foreach(var os in OSVersions)
43+
{
44+
Log<TestCommandFeature>.G().LogInformation($"Creating matrix for {os}");
45+
46+
configs.Add(new
47+
{
48+
codeql_cli = config.CodeQLCLI,
49+
codeql_standard_library = config.CodeQLStandardLibrary,
50+
codeql_cli_bundle = config.CodeQLCLIBundle,
51+
os = os,
52+
codeql_standard_library_ident = $"codeql-cli_v{config.CodeQLCLI}"
53+
});
54+
}
55+
56+
var data = new
57+
{
58+
include = configs
59+
};
60+
61+
var json = JsonSerializer.Serialize(data);
62+
Console.WriteLine(json);
63+
64+
}
65+
}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using CodeQLToolkit.Features.Test.Lifecycle;
2+
using CodeQLToolkit.Shared.Types;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.CommandLine;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace CodeQLToolkit.Features.Test.Commands
11+
{
12+
public class TestCommandFeature : FeatureBase, IToolkitLifecycleFeature
13+
{
14+
public TestCommandFeature()
15+
{
16+
FeatureName = "Test";
17+
}
18+
19+
public void Register(Command parentCommand)
20+
{
21+
Log<TestCommandFeature>.G().LogInformation("Registering command submodule.");
22+
23+
24+
var runCommand = new Command("run", "Functions pertaining to running test-related commands.");
25+
parentCommand.Add(runCommand);
26+
27+
// a command that installs query packs
28+
var getMatrixTestCommand = new Command("get-matrix", "Gets a CI/CD matrix based on the current configuration.");
29+
30+
var matrixOSVersion = new Option<string>("--os-version", () => "ubuntu-latest", "A comma-seperated list of operating systems to use. Example: `ubuntu-latest`.") { IsRequired = true };
31+
getMatrixTestCommand.Add(matrixOSVersion);
32+
33+
runCommand.Add(getMatrixTestCommand);
34+
35+
getMatrixTestCommand.SetHandler((basePath, automationType, osVersions) => {
36+
37+
Log<TestCommandFeature>.G().LogInformation("Executing get-matrix command...");
38+
39+
// dispatch at runtime to the correct automation type
40+
var featureTarget = AutomationFeatureFinder.FindTargetForAutomationType<BaseGetMatrixCommandTarget>(AutomationTypeHelper.AutomationTypeFromString(automationType));
41+
42+
featureTarget.Base = basePath;
43+
featureTarget.OSVersions = osVersions.Split(",");
44+
45+
featureTarget.Run();
46+
47+
}, Globals.BasePathOption, Globals.AutomationTypeOption, matrixOSVersion);
48+
}
49+
50+
public int Run()
51+
{
52+
throw new NotImplementedException();
53+
}
54+
}
55+
}

src/CodeQLToolkit.Features.Test/Lifecycle/BaseLifecycleTarget.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@ namespace CodeQLToolkit.Features.Test.Lifecycle
88
{
99
abstract public class BaseLifecycleTarget : ILifecycleTarget
1010
{
11-
public string Base { get; set; }
12-
13-
1411
}
1512
}

src/CodeQLToolkit.Features.Test/Lifecycle/TestLifecycleFeature.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ public void Register(Command parentCommand)
2525
{
2626
Log<TestLifecycleFeature>.G().LogInformation("Executing init command...");
2727

28-
29-
var attribute = typeof(InitLifecycleTarget).GetCustomAttribute(typeof(AutomationTypeAttribute));
30-
31-
3228
// dispatch at runtime to the correct automation type
29+
3330
var featureTarget = AutomationFeatureFinder.FindTargetForAutomationType<BaseLifecycleTarget>(AutomationTypeHelper.AutomationTypeFromString(automationType));
3431

3532
featureTarget.Base = basePath;

src/CodeQLToolkit.Features.Test/TestFeatureMain.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CodeQLToolkit.Features.Test.Lifecycle;
1+
using CodeQLToolkit.Features.Test.Commands;
2+
using CodeQLToolkit.Features.Test.Lifecycle;
23
using CodeQLToolkit.Shared.Feature;
34
using System.CommandLine;
45

@@ -8,6 +9,7 @@ public class TestFeatureMain : IToolkitFeature
89

910
{
1011
readonly TestLifecycleFeature lifecycleFeature;
12+
readonly TestCommandFeature commandFeature;
1113
readonly static TestFeatureMain instance;
1214

1315
static TestFeatureMain()
@@ -18,15 +20,21 @@ static TestFeatureMain()
1820
private TestFeatureMain()
1921
{
2022
lifecycleFeature = new TestLifecycleFeature();
23+
commandFeature = new TestCommandFeature();
2124
}
2225
public static TestFeatureMain Instance { get { return instance; } }
2326

2427
public void Register(Command parentCommand)
2528
{
2629
var testCommand = new Command("test", "Features related to the running and processing of CodeQL Unit Tests.");
2730
parentCommand.Add(testCommand);
31+
2832
Log<TestFeatureMain>.G().LogInformation("Registering scaffolding submodule.");
2933
lifecycleFeature.Register(testCommand);
34+
35+
Log<TestFeatureMain>.G().LogInformation("Registering command submodule.");
36+
commandFeature.Register(testCommand);
37+
3038
}
3139

3240
public int Run()

supported_codeql_configs.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)