Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Supporting deep comparison of tasks with reference value in custom properties #9647

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions packages/task/src/browser/task-definition-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

import { expect } from 'chai';
import { PanelKind, RevealKind, TaskScope } from '../common';
import { TaskDefinitionRegistry } from './task-definition-registry';

/* eslint-disable no-unused-expressions */
Expand Down Expand Up @@ -54,6 +55,52 @@ describe('TaskDefinitionRegistry', () => {
}
}
};
const FAKE_TASK_META = {
TYPE: 'foobar_type',
SRC: 'foobar_src'
};
const defaultPresentation = {
clear: false,
echo: true,
focus: false,
panel: PanelKind.Shared,
reveal: RevealKind.Always,
showReuseMessage: true,
};
const fakeTaskContrib = {
def: {
taskType: FAKE_TASK_META.TYPE,
source: FAKE_TASK_META.SRC,
required: ['strArg'],
properties: {
required: ['strArg'],
all: ['strArg', 'arrArgs'],
schema: {
type: FAKE_TASK_META.TYPE,
required: ['strArg'],
properties: {
strArg: {},
arrArgs: {}
}
}
}
},
conf: (
executionId = 'foobar',
type = FAKE_TASK_META.TYPE,
_source = FAKE_TASK_META.SRC,
arrArgs: unknown[] = [],
strArg = '',
label = 'foobar',
presentation = defaultPresentation,
problemMatcher = undefined,
taskType = 'customExecution',
_scope = TaskScope.Workspace,
) => ({
executionId, arrArgs, strArg, label, presentation,
problemMatcher, taskType, type, _scope, _source,
})
};

beforeEach(() => {
registry = new TaskDefinitionRegistry();
Expand Down Expand Up @@ -105,4 +152,54 @@ describe('TaskDefinitionRegistry', () => {
expect(defs2!.taskType).to.be.eq(definitionContributionB.taskType);
});
});

describe('compareTasks function', () => {

beforeEach(() => registry.register(fakeTaskContrib.def));

it('should return false if given 2 task configurations with different type', () => {
const areSameTasks = registry.compareTasks(
fakeTaskContrib.conf('id_1', 'type_1'),
fakeTaskContrib.conf('id_2', 'type_2'),
);
expect(areSameTasks).to.be.false;
});

it('should return true if given 2 same task configurations with empty arrays (different by reference) as custom property', () => {
const areSameTasks = registry.compareTasks(
fakeTaskContrib.conf('id_1'),
fakeTaskContrib.conf('id_2'),
);
expect(areSameTasks).to.be.true;
});

it('should return true if given 2 same task configurations with deep properties (different by reference)', () => {
const areSameTasks = registry.compareTasks(
fakeTaskContrib.conf('id_1', undefined, undefined, [1, '2', { '3': { a: true, b: 'string' } }]),
fakeTaskContrib.conf('id_2', undefined, undefined, [1, '2', { '3': { a: true, b: 'string' } }]),
);
expect(areSameTasks).to.be.true;
});

it('should return false if given 2 task configurations with different deep properties', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const inputs: [any, any][] = [
[
fakeTaskContrib.conf('id_1', undefined, undefined, [1, '2', { '3': { a: true, b: 'b' } }]),
fakeTaskContrib.conf('id_2', undefined, undefined, [1, '2', { '3': { a: true } }]),
],
[
fakeTaskContrib.conf('id_1', undefined, undefined, [1, '2']),
fakeTaskContrib.conf('id_2', undefined, undefined, [1, 2]),
],
[
// eslint-disable-next-line no-null/no-null
fakeTaskContrib.conf('id_1', undefined, undefined, [1, '2', { c: null }]),
fakeTaskContrib.conf('id_2', undefined, undefined, [1, '2', { c: undefined }]),
],
];
const allAreFalse = inputs.map(args => registry.compareTasks(...args)).every(areSameTasks => areSameTasks === false);
expect(allAreFalse).to.be.true;
});
});
});
3 changes: 2 additions & 1 deletion packages/task/src/browser/task-definition-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

import { injectable } from '@theia/core/shared/inversify';
import { JSONExt } from '@theia/core/shared/@phosphor/coreutils';
import { Event, Emitter } from '@theia/core/lib/common';
import { TaskConfiguration, TaskDefinition, TaskCustomization } from '../common';
import { Disposable } from '@theia/core/lib/common/disposable';
Expand Down Expand Up @@ -119,7 +120,7 @@ export class TaskDefinitionRegistry {
// "scope" reflects the original TaskConfigurationScope as provided by plugins,
// Matching "_scope" or "scope" are both accepted in order to correlate provided task
// configurations (e.g. TaskScope.Workspace) against already configured tasks.
return def.properties.all.every(p => p === 'type' || one[p] === other[p])
return def.properties.all.every(p => p === 'type' || JSONExt.deepEqual(one[p], other[p]))
&& (one._scope === other._scope || one.scope === other.scope);
}
return one.label === other.label && one._source === other._source;
Expand Down