Skip to content

Commit 7bb6859

Browse files
committed
fix(toolkit): handle isCn when region is not initialized with unit test
cases
1 parent ded7bf2 commit 7bb6859

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

packages/core/src/shared/extensionUtilities.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,14 @@ export function isSageMaker(appName: 'SMAI' | 'SMUS' = 'SMAI'): boolean {
195195

196196
export function isCn(): boolean {
197197
try {
198-
return getComputeRegion()?.startsWith('cn') ?? false
199-
} catch (e) {
200-
// If compute region isn't initialized yet, assume we're not in a CN region
201-
getLogger().debug('isCn called before compute region initialized, defaulting to false')
198+
const region = getComputeRegion()
199+
if (!region || region === 'notInitialized') {
200+
getLogger().debug('isCn called before compute region initialized, defaulting to false')
201+
return false
202+
}
203+
return region.startsWith('cn')
204+
} catch (err) {
205+
getLogger().error(`Error in isCn method: ${err}`)
202206
return false
203207
}
204208
}

packages/core/src/test/shared/extensionUtilities.test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AWSError } from 'aws-sdk'
99
import * as sinon from 'sinon'
1010
import { DefaultEc2MetadataClient } from '../../shared/clients/ec2MetadataClient'
1111
import * as vscode from 'vscode'
12-
import { UserActivity, getComputeRegion, initializeComputeRegion } from '../../shared/extensionUtilities'
12+
import { UserActivity, getComputeRegion, initializeComputeRegion, isCn } from '../../shared/extensionUtilities'
1313
import { isDifferentVersion, setMostRecentVersion } from '../../shared/extensionUtilities'
1414
import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
1515
import { extensionVersion } from '../../shared/vscode/env'
@@ -135,6 +135,65 @@ describe('initializeComputeRegion, getComputeRegion', async function () {
135135
})
136136
})
137137

138+
describe('isCn', function () {
139+
let sandbox: sinon.SinonSandbox
140+
const metadataService = new DefaultEc2MetadataClient()
141+
142+
beforeEach(function () {
143+
sandbox = sinon.createSandbox()
144+
})
145+
146+
afterEach(function () {
147+
sandbox.restore()
148+
})
149+
150+
it('returns false when compute region is not initialized', async function () {
151+
// Reset the compute region to undefined first
152+
const utils = require('../../shared/extensionUtilities')
153+
Object.defineProperty(utils, 'computeRegion', {
154+
value: undefined,
155+
configurable: true,
156+
})
157+
158+
const result = isCn()
159+
160+
assert.strictEqual(result, false, 'isCn() should return false when compute region is not initialized')
161+
})
162+
163+
it('returns true for CN regions', async function () {
164+
sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'cn-north-1' })
165+
await initializeComputeRegion(metadataService, false, true)
166+
167+
const result = isCn()
168+
169+
assert.strictEqual(result, true, 'isCn() should return true for China regions')
170+
})
171+
172+
it('returns false for non-CN regions', async function () {
173+
sandbox.stub(metadataService, 'getInstanceIdentity').resolves({ region: 'us-east-1' })
174+
await initializeComputeRegion(metadataService, false, true)
175+
176+
const result = isCn()
177+
178+
assert.strictEqual(result, false, 'isCn() should return false for non-China regions')
179+
})
180+
181+
it('returns false when an error occurs', async function () {
182+
const utils = require('../../shared/extensionUtilities')
183+
184+
// Restore original getComputeRegion if it was stubbed
185+
if (utils.getComputeRegion.restore) {
186+
utils.getComputeRegion.restore()
187+
}
188+
189+
sandbox.stub(utils, 'getComputeRegion').throws(new Error('Test error'))
190+
191+
const result = isCn()
192+
193+
assert.strictEqual(result, false, 'isCn() should return false when an error occurs')
194+
})
195+
})
196+
138197
describe('UserActivity', function () {
139198
let count: number
140199
let sandbox: sinon.SinonSandbox

0 commit comments

Comments
 (0)