Skip to content

Commit 9be5bbf

Browse files
authored
test(integ): fix getDefaultSchemas() #3236
Problem: Test fails in CI because of unpredictable telemetry and extension activation. Solution: - Avoid `FakeExtensionContext` in integ tests. - In test 1: assert telemetry from extension activation. - In test 2: clear telemetry.
1 parent f740730 commit 9be5bbf

File tree

4 files changed

+31
-38
lines changed

4 files changed

+31
-38
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export async function activate(context: vscode.ExtensionContext) {
119119
globals.loginManager = loginManager
120120
globals.awsContextCommands = new AwsContextCommands(regionProvider, Auth.instance)
121121
globals.sdkClientBuilder = new DefaultAWSClientBuilder(awsContext)
122-
globals.schemaService = new SchemaService(context)
122+
globals.schemaService = new SchemaService()
123123
globals.resourceManager = new AwsResourceManager(context)
124124

125125
const settings = Settings.instance

src/integrationTest/schema/schema.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
import globals from '../../shared/extensionGlobals'
67
import { getDefaultSchemas, samAndCfnSchemaUrl } from '../../shared/schemas'
7-
import { FakeExtensionContext } from '../../test/fakeExtensionContext'
88
import {
99
getCITestSchemas,
1010
JSONObject,
@@ -66,14 +66,10 @@ describe('Sam Schema Regression', function () {
6666
})
6767

6868
describe('getDefaultSchemas()', () => {
69-
let extensionContext: FakeExtensionContext
70-
71-
beforeEach(async () => {
72-
extensionContext = await FakeExtensionContext.create()
73-
})
69+
beforeEach(async () => {})
7470

7571
it('uses cache on subsequent request for CFN/SAM schema', async () => {
76-
await getDefaultSchemas(extensionContext)
72+
await getDefaultSchemas()
7773

7874
// IMPORTANT: Since CFN and SAM use the same schema, the order their schema is retrieved is irrelevant
7975

@@ -92,20 +88,24 @@ describe('getDefaultSchemas()', () => {
9288
})
9389

9490
it('uses cache for all requests on second function call', async () => {
95-
await getDefaultSchemas(extensionContext)
91+
globals.telemetry.telemetryEnabled = true
92+
globals.telemetry.clearRecords()
93+
globals.telemetry.logger.clear()
94+
95+
await getDefaultSchemas()
9696
// Call a second time
97-
await getDefaultSchemas(extensionContext)
97+
await getDefaultSchemas()
9898

9999
// Only cache is used
100100
assertTelemetry(
101101
'toolkit_getExternalResource',
102102
{ url: samAndCfnSchemaUrl, passive: true, result: 'Cancelled', reason: 'Cache hit' },
103-
2
103+
0
104104
)
105105
assertTelemetry(
106106
'toolkit_getExternalResource',
107107
{ url: samAndCfnSchemaUrl, passive: true, result: 'Cancelled', reason: 'Cache hit' },
108-
3
108+
1
109109
)
110110
})
111111
})

src/shared/schemas.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import { SystemUtilities } from './systemUtilities'
1919
import { normalizeVSCodeUri } from './utilities/vsCodeUtils'
2020
import { telemetry } from './telemetry/telemetry'
2121

22-
// Note: this file is currently 12+ MB. When requesting it, specify compression/gzip.
23-
export const samAndCfnSchemaUrl = 'https://raw.githubusercontent.com/aws/serverless-application-model/main/samtranslator/schema/schema.json'
22+
// Note: this file is currently 12+ MB. When requesting it, specify compression/gzip.
23+
export const samAndCfnSchemaUrl =
24+
'https://raw.githubusercontent.com/aws/serverless-application-model/main/samtranslator/schema/schema.json'
2425
const devfileManifestUrl = 'https://api.github.com/repos/devfile/api/releases/latest'
2526
const schemaPrefix = `${AWS_SCHEME}://`
2627

@@ -53,15 +54,12 @@ export class SchemaService {
5354
private schemas?: Schemas
5455
private handlers: Map<SchemaType, SchemaHandler>
5556

56-
public constructor(
57-
private readonly extensionContext: vscode.ExtensionContext,
58-
opts?: {
59-
/** Assigned in start(). */
60-
schemas?: Schemas
61-
updatePeriod?: number
62-
handlers?: Map<SchemaType, SchemaHandler>
63-
}
64-
) {
57+
public constructor(opts?: {
58+
/** Assigned in start(). */
59+
schemas?: Schemas
60+
updatePeriod?: number
61+
handlers?: Map<SchemaType, SchemaHandler>
62+
}) {
6563
this.updatePeriod = opts?.updatePeriod ?? SchemaService.defaultUpdatePeriodMillis
6664
this.schemas = opts?.schemas
6765
this.handlers =
@@ -82,7 +80,7 @@ export class SchemaService {
8280
}
8381

8482
public async start(): Promise<void> {
85-
getDefaultSchemas(this.extensionContext).then(schemas => (this.schemas = schemas))
83+
getDefaultSchemas().then(schemas => (this.schemas = schemas))
8684
await this.startTimer()
8785
}
8886

@@ -139,14 +137,13 @@ export class SchemaService {
139137
* Checks manifest and downloads new schemas if the manifest version has been bumped.
140138
* Uses local, predownloaded version if up-to-date or network call fails
141139
* If the user has not previously used the toolkit and cannot pull the manifest, does not provide template autocomplete.
142-
* @param extensionContext VSCode extension context
143140
*/
144-
export async function getDefaultSchemas(extensionContext: vscode.ExtensionContext): Promise<Schemas | undefined> {
145-
const devfileSchemaUri = vscode.Uri.joinPath(extensionContext.globalStorageUri, 'devfile.schema.json')
141+
export async function getDefaultSchemas(): Promise<Schemas | undefined> {
142+
const devfileSchemaUri = vscode.Uri.joinPath(globals.context.globalStorageUri, 'devfile.schema.json')
146143
const devfileSchemaVersion = await getPropertyFromJsonUrl(devfileManifestUrl, 'tag_name')
147144

148145
// Sam schema is a superset of Cfn schema, so we can use it for both
149-
const samAndCfnSchemaDestinationUri = vscode.Uri.joinPath(extensionContext.globalStorageUri, 'sam.schema.json')
146+
const samAndCfnSchemaDestinationUri = vscode.Uri.joinPath(globals.context.globalStorageUri, 'sam.schema.json')
150147
const samAndCfnCacheKey = 'samAndCfnSchemaVersion'
151148

152149
const schemas: Schemas = {}
@@ -157,7 +154,7 @@ export async function getDefaultSchemas(extensionContext: vscode.ExtensionContex
157154
eTag: undefined,
158155
url: samAndCfnSchemaUrl,
159156
cacheKey: samAndCfnCacheKey,
160-
extensionContext,
157+
extensionContext: globals.context,
161158
title: schemaPrefix + 'cloudformation.schema.json',
162159
})
163160
schemas['cfn'] = samAndCfnSchemaDestinationUri
@@ -171,7 +168,7 @@ export async function getDefaultSchemas(extensionContext: vscode.ExtensionContex
171168
eTag: undefined,
172169
url: samAndCfnSchemaUrl,
173170
cacheKey: samAndCfnCacheKey,
174-
extensionContext,
171+
extensionContext: globals.context,
175172
title: schemaPrefix + 'sam.schema.json',
176173
})
177174
schemas['sam'] = samAndCfnSchemaDestinationUri
@@ -185,7 +182,7 @@ export async function getDefaultSchemas(extensionContext: vscode.ExtensionContex
185182
version: devfileSchemaVersion,
186183
url: `https://raw.githubusercontent.com/devfile/api/${devfileSchemaVersion}/schemas/latest/devfile.json`,
187184
cacheKey: 'devfileSchemaVersion',
188-
extensionContext,
185+
extensionContext: globals.context,
189186
title: schemaPrefix + 'devfile.schema.json',
190187
})
191188
schemas['devfile'] = devfileSchemaUri

src/test/shared/schemas.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import * as vscode from 'vscode'
77
import * as assert from 'assert'
88
import { anything, deepEqual, instance, mock, verify } from '../utilities/mockito'
9-
import { ExtensionContext } from 'vscode'
109
import { YamlExtension } from '../../shared/extensions/yaml'
1110
import {
1211
JsonSchemaHandler,
@@ -16,23 +15,20 @@ import {
1615
SchemaType,
1716
YamlSchemaHandler,
1817
} from '../../shared/schemas'
19-
import { FakeExtensionContext } from '../fakeExtensionContext'
2018
import { Settings } from '../../shared/settings'
2119

2220
describe('SchemaService', function () {
2321
let service: SchemaService
24-
let fakeExtensionContext: ExtensionContext
2522
let config: Settings
2623
let fakeYamlExtension: YamlExtension
2724
const cfnSchema = vscode.Uri.file('cfn')
2825
const samSchema = vscode.Uri.file('sam')
2926

3027
beforeEach(async function () {
31-
fakeExtensionContext = await FakeExtensionContext.create()
3228
fakeYamlExtension = mock()
3329
config = new Settings(vscode.ConfigurationTarget.Workspace)
3430

35-
service = new SchemaService(fakeExtensionContext, {
31+
service = new SchemaService({
3632
schemas: {
3733
cfn: cfnSchema,
3834
sam: samSchema,
@@ -105,7 +101,7 @@ describe('SchemaService', function () {
105101

106102
it('processes no updates if schemas are unavailable', async function () {
107103
fakeYamlExtension = mock()
108-
service = new SchemaService(fakeExtensionContext, {
104+
service = new SchemaService({
109105
handlers: new Map<SchemaType, SchemaHandler>([
110106
['json', new JsonSchemaHandler()],
111107
['yaml', new YamlSchemaHandler(instance(fakeYamlExtension))],
@@ -123,7 +119,7 @@ describe('SchemaService', function () {
123119

124120
it('processes no updates if yaml extension unavailable', async function () {
125121
fakeYamlExtension = mock()
126-
service = new SchemaService(fakeExtensionContext)
122+
service = new SchemaService()
127123

128124
service.registerMapping({
129125
uri: vscode.Uri.parse('/foo'),

0 commit comments

Comments
 (0)