Skip to content

Commit

Permalink
[INTERNAL] TaskRunner: Use SpecVersionComparator
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Nov 25, 2022
1 parent e4c2433 commit 0dbaae6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
5 changes: 3 additions & 2 deletions lib/build/TaskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,13 @@ class TaskRunner {
params.dependencies = dependencies;
}

if (task.getSpecVersion() === "3.0") {
const specVersion = task.getSpecVersionComparator();
if (specVersion.gte("3.0")) {
params.options.taskName = newTaskName;
params.log = logger.getGroupLogger(`builder:custom-task:${newTaskName}`);
}

const taskUtilInterface = taskUtil.getInterface(task.getSpecVersionComparator());
const taskUtilInterface = taskUtil.getInterface(specVersion);
// Interface is undefined if specVersion does not support taskUtil
if (taskUtilInterface) {
params.taskUtil = taskUtilInterface;
Expand Down
42 changes: 30 additions & 12 deletions test/lib/build/TaskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,13 @@ test("Custom tasks is unknown", async (t) => {
test("Custom task is called correctly", async (t) => {
const {sinon, graph, taskUtil, taskRepository, TaskRunner} = t.context;
const taskStub = sinon.stub();
const mockSpecVersionComparator = {
gte: () => false
};
graph.getExtension.returns({
getTask: () => taskStub,
getSpecVersion: () => "2.6",
getSpecVersionComparator: () => "specVersionComparator 2.6"
getSpecVersionComparator: () => mockSpecVersionComparator
});
t.context.taskUtil.getInterface.returns("taskUtil interface");
const project = getMockProject("module");
Expand Down Expand Up @@ -427,17 +430,20 @@ test("Custom task is called correctly", async (t) => {
}, "Task got called with one argument");

t.is(taskUtil.getInterface.callCount, 1, "taskUtil#getInterface got called once");
t.is(taskUtil.getInterface.getCall(0).args[0], "specVersionComparator 2.6",
t.is(taskUtil.getInterface.getCall(0).args[0], mockSpecVersionComparator,
"taskUtil#getInterface got called with correct argument");
});

test("Custom task with legacy spec version", async (t) => {
const {sinon, graph, taskUtil, taskRepository, TaskRunner} = t.context;
const taskStub = sinon.stub();
const mockSpecVersionComparator = {
gte: () => false
};
graph.getExtension.returns({
getTask: () => taskStub,
getSpecVersion: () => "1.0",
getSpecVersionComparator: () => "specVersionComparator 1.0"
getSpecVersionComparator: () => mockSpecVersionComparator
});
t.context.taskUtil.getInterface.returns(undefined); // simulating no taskUtil for old specVersion
const project = getMockProject("module");
Expand Down Expand Up @@ -469,17 +475,20 @@ test("Custom task with legacy spec version", async (t) => {
}, "Task got called with one argument");

t.is(taskUtil.getInterface.callCount, 1, "taskUtil#getInterface got called once");
t.is(taskUtil.getInterface.getCall(0).args[0], "specVersionComparator 1.0",
t.is(taskUtil.getInterface.getCall(0).args[0], mockSpecVersionComparator,
"taskUtil#getInterface got called with correct argument");
});

test("Custom task with specVersion 3.0", async (t) => {
const {sinon, graph, taskUtil, taskRepository, TaskRunner} = t.context;
const taskStub = sinon.stub();
const mockSpecVersionComparator = {
gte: () => true
};
graph.getExtension.returns({
getTask: () => taskStub,
getSpecVersion: () => "3.0",
getSpecVersionComparator: () => "specVersionComparator 3.0"
getSpecVersionComparator: () => mockSpecVersionComparator
});
t.context.taskUtil.getInterface.returns(undefined); // simulating no taskUtil for old specVersion
const project = getMockProject("module");
Expand Down Expand Up @@ -513,7 +522,7 @@ test("Custom task with specVersion 3.0", async (t) => {
}, "Task got called with one argument");

t.is(taskUtil.getInterface.callCount, 1, "taskUtil#getInterface got called once");
t.is(taskUtil.getInterface.getCall(0).args[0], "specVersionComparator 3.0",
t.is(taskUtil.getInterface.getCall(0).args[0], mockSpecVersionComparator,
"taskUtil#getInterface got called with correct argument");
});

Expand All @@ -522,20 +531,29 @@ test("Multiple custom tasks with same name are called correctly", async (t) => {
const taskStub1 = sinon.stub();
const taskStub2 = sinon.stub();
const taskStub3 = sinon.stub();
const mockSpecVersionComparatorA = {
gte: () => false
};
const mockSpecVersionComparatorB = {
gte: () => false
};
const mockSpecVersionComparatorC = {
gte: () => true
};
graph.getExtension.onFirstCall().returns({
getTask: () => taskStub1,
getSpecVersion: () => "2.5",
getSpecVersionComparator: () => "specVersionComparator 2.5"
getSpecVersionComparator: () => mockSpecVersionComparatorA
});
graph.getExtension.onSecondCall().returns({
getTask: () => taskStub2,
getSpecVersion: () => "2.6",
getSpecVersionComparator: () => "specVersionComparator 2.6"
getSpecVersionComparator: () => mockSpecVersionComparatorB
});
graph.getExtension.onThirdCall().returns({
getTask: () => taskStub3,
getSpecVersion: () => "3.0",
getSpecVersionComparator: () => "specVersionComparator 3.0"
getSpecVersionComparator: () => mockSpecVersionComparatorC
});
const project = getMockProject("module");
project.getCustomTasks = () => [
Expand Down Expand Up @@ -605,11 +623,11 @@ test("Multiple custom tasks with same name are called correctly", async (t) => {
}, "Task 3 got called with one argument");

t.is(taskUtil.getInterface.callCount, 3, "taskUtil#getInterface got called once");
t.is(taskUtil.getInterface.getCall(0).args[0], "specVersionComparator 2.5",
t.is(taskUtil.getInterface.getCall(0).args[0], mockSpecVersionComparatorA,
"taskUtil#getInterface got called with correct argument on first call");
t.is(taskUtil.getInterface.getCall(1).args[0], "specVersionComparator 3.0",
t.is(taskUtil.getInterface.getCall(1).args[0], mockSpecVersionComparatorC,
"taskUtil#getInterface got called with correct argument on second call");
t.is(taskUtil.getInterface.getCall(2).args[0], "specVersionComparator 2.6",
t.is(taskUtil.getInterface.getCall(2).args[0], mockSpecVersionComparatorB,
"taskUtil#getInterface got called with correct argument on third call");
});

Expand Down

0 comments on commit 0dbaae6

Please sign in to comment.