diff --git a/public/app/plugins/datasource/influxdb/datasource.test.ts b/public/app/plugins/datasource/influxdb/datasource.test.ts index 4633f20744a..8acca0c7ec5 100644 --- a/public/app/plugins/datasource/influxdb/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/datasource.test.ts @@ -273,6 +273,15 @@ describe('interpolateQueryExpr', () => { replace: jest.fn().mockImplementation((...rest: unknown[]) => 'templateVarReplaced'), } as unknown as TemplateSrv; let ds = getMockInfluxDS(getMockDSInstanceSettings(), templateSrvStub); + + // Mock console.warn as we expect tests to use it + beforeEach(() => { + jest.spyOn(console, 'warn').mockImplementation(); + }); + afterEach(() => { + jest.restoreAllMocks(); + }); + it('should return the value as it is', () => { const value = 'normalValue'; const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); @@ -281,6 +290,18 @@ describe('interpolateQueryExpr', () => { expect(result).toBe(expectation); }); + it('should return the escaped value if the value wrapped in regex without !~ or =~', () => { + const value = '/special/path'; + const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); + const result = ds.interpolateQueryExpr( + value, + variableMock, + 'select atan(z/sqrt(3.14)), that where path /$tempVar/' + ); + const expectation = `\\/special\\/path`; + expect(result).toBe(expectation); + }); + it('should return the escaped value if the value wrapped in regex', () => { const value = '/special/path'; const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index a21031f20ab..92d26d51058 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -347,10 +347,7 @@ export default class InfluxDatasource extends DataSourceWithBackend escapeRegex(v)).join('|')})`; + // If the value is a string array first escape them then join them with pipe + // then put inside parenthesis. + return typeof value === 'string' ? escapeRegex(value) : `(${value.map((v) => escapeRegex(v)).join('|')})`; + } catch (e) { + console.warn(`Supplied match is not valid regex: ${match}`); + } } return value;