From 6c865dbd1d5c927b664044bca7e09dae63741996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Fri, 6 Mar 2026 14:24:43 +0100 Subject: [PATCH] fix: use AWS_SHARED_CREDENTIALS_FILE instead of smithy internals in tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the brittle @smithy/shared-ini-file-loader internals hack with temp credential files and AWS_SHARED_CREDENTIALS_FILE/AWS_CONFIG_FILE env vars, which is the official AWS SDK mechanism for this. Co-Authored-By: Claude Opus 4.6 Signed-off-by: Fredrik Adelöw --- packages/integration-aws-node/package.json | 1 - .../src/DefaultAwsCredentialsManager.test.ts | 34 ++++++++++--------- yarn.lock | 1 - 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/integration-aws-node/package.json b/packages/integration-aws-node/package.json index 0f71c487ef..6070a3eea0 100644 --- a/packages/integration-aws-node/package.json +++ b/packages/integration-aws-node/package.json @@ -48,7 +48,6 @@ "@backstage/cli": "workspace:^", "@backstage/config-loader": "workspace:^", "@backstage/test-utils": "workspace:^", - "@smithy/shared-ini-file-loader": "^4.4.6", "aws-sdk-client-mock": "^4.0.0", "aws-sdk-client-mock-jest": "^4.0.0" }, diff --git a/packages/integration-aws-node/src/DefaultAwsCredentialsManager.test.ts b/packages/integration-aws-node/src/DefaultAwsCredentialsManager.test.ts index e8d295c200..1c22a71a16 100644 --- a/packages/integration-aws-node/src/DefaultAwsCredentialsManager.test.ts +++ b/packages/integration-aws-node/src/DefaultAwsCredentialsManager.test.ts @@ -30,11 +30,15 @@ import { } from '@aws-sdk/nested-clients/sts'; import { Config, ConfigReader } from '@backstage/config'; import { fromNodeProviderChain } from '@aws-sdk/credential-providers'; +import { join } from 'node:path'; +import { mkdtempSync, writeFileSync, rmSync } from 'node:fs'; +import { tmpdir } from 'node:os'; const env = process.env; let stsMock: AwsClientStub; let nestedStsMock: AwsClientStub; let config: Config; +let tmpDir: string; jest.mock('@aws-sdk/credential-providers', () => { const originalModule = jest.requireActual('@aws-sdk/credential-providers'); @@ -175,26 +179,24 @@ describe('DefaultAwsCredentialsManager', () => { jest.requireActual('@aws-sdk/credential-providers').fromNodeProviderChain, ); - // Inject mock credentials file via @smithy/shared-ini-file-loader's fileIntercept - const mockProfile = `[my-profile]\naws_access_key_id=ACCESS_KEY_ID_9\naws_secret_access_key=SECRET_ACCESS_KEY_9\n`; - const sharedIniReadFile = require('@smithy/shared-ini-file-loader/dist-cjs/readFile'); - // Clear cached file promises so re-reads pick up the intercept - for (const key of Object.keys(sharedIniReadFile.filePromises)) { - delete sharedIniReadFile.filePromises[key]; - } - for (const key of Object.keys(sharedIniReadFile.fileIntercept)) { - delete sharedIniReadFile.fileIntercept[key]; - } - // Set intercepts for both the credentials and config file paths - const homeDir = require('node:os').homedir(); - const credPath = `${homeDir}/.aws/credentials`; - const configPath = `${homeDir}/.aws/config`; - sharedIniReadFile.fileIntercept[credPath] = Promise.resolve(mockProfile); - sharedIniReadFile.fileIntercept[configPath] = Promise.resolve(''); + // Write a temporary AWS credentials file and point the SDK at it + tmpDir = mkdtempSync(join(tmpdir(), 'aws-test-')); + const credFilePath = join(tmpDir, 'credentials'); + const configFilePath = join(tmpDir, 'config'); + writeFileSync( + credFilePath, + '[my-profile]\naws_access_key_id=ACCESS_KEY_ID_9\naws_secret_access_key=SECRET_ACCESS_KEY_9\n', + ); + writeFileSync(configFilePath, ''); + process.env.AWS_SHARED_CREDENTIALS_FILE = credFilePath; + process.env.AWS_CONFIG_FILE = configFilePath; }); afterEach(() => { process.env = env; + if (tmpDir) { + rmSync(tmpDir, { recursive: true, force: true }); + } }); describe('#getCredentialProvider', () => { diff --git a/yarn.lock b/yarn.lock index 4b6be16ac0..ffed4a9193 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4429,7 +4429,6 @@ __metadata: "@backstage/config-loader": "workspace:^" "@backstage/errors": "workspace:^" "@backstage/test-utils": "workspace:^" - "@smithy/shared-ini-file-loader": "npm:^4.4.6" aws-sdk-client-mock: "npm:^4.0.0" aws-sdk-client-mock-jest: "npm:^4.0.0" languageName: unknown