fix: use AWS_SHARED_CREDENTIALS_FILE instead of smithy internals in tests
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 <noreply@anthropic.com> Signed-off-by: Fredrik Adelöw <freben@spotify.com>
This commit is contained in:
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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<STSClient>;
|
||||
let nestedStsMock: AwsClientStub<NestedSTSClient>;
|
||||
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', () => {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user