-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script to auto generate new-client tests (#204)
- Loading branch information
Showing
25 changed files
with
522 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
scripts/generateNewClientTests/getClientNamesSortedByPackageName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { CLIENT_PACKAGE_NAMES_MAP } from "../../src/transforms/v2-to-v3/utils/config"; | ||
|
||
export const getClientNamesSortedByPackageName = () => | ||
Object.keys(CLIENT_PACKAGE_NAMES_MAP).sort((a, b) => | ||
CLIENT_PACKAGE_NAMES_MAP[a].localeCompare(CLIENT_PACKAGE_NAMES_MAP[b]) | ||
); |
10 changes: 10 additions & 0 deletions
10
scripts/generateNewClientTests/getGlobalImportInputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getGlobalImportInputContent = (codegenComment: string) => { | ||
let globalImportInputContent = `${codegenComment}\n`; | ||
|
||
globalImportInputContent += `import AWS from "aws-sdk";\n\n`; | ||
globalImportInputContent += getV2ClientsNewExpressionCode(`AWS.`); | ||
|
||
return globalImportInputContent; | ||
}; |
13 changes: 13 additions & 0 deletions
13
scripts/generateNewClientTests/getGlobalImportOutputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; | ||
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; | ||
import { getV3PackageImportsCode } from "./getV3PackageImportsCode"; | ||
|
||
export const getGlobalImportOutputContent = (codegenComment: string) => { | ||
let globalImportOutputContent = `${codegenComment}\n`; | ||
|
||
globalImportOutputContent += getV3PackageImportsCode(getClientNamesSortedByPackageName()); | ||
globalImportOutputContent += `\n`; | ||
globalImportOutputContent += getV3ClientsNewExpressionCode(); | ||
|
||
return globalImportOutputContent; | ||
}; |
10 changes: 10 additions & 0 deletions
10
scripts/generateNewClientTests/getGlobalRequireInputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getGlobalRequireInputContent = (codegenComment: string) => { | ||
let globalRequireInputContent = `${codegenComment}\n`; | ||
|
||
globalRequireInputContent += `const AWS = require("aws-sdk");\n\n`; | ||
globalRequireInputContent += getV2ClientsNewExpressionCode(`AWS.`); | ||
|
||
return globalRequireInputContent; | ||
}; |
14 changes: 14 additions & 0 deletions
14
scripts/generateNewClientTests/getGlobalRequireOutputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; | ||
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; | ||
import { getV3PackageRequireCode } from "./getV3PackageRequireCode"; | ||
|
||
export const getGlobalRequireOutputContent = (codegenComment: string) => { | ||
let globalRequireOutputContent = `${codegenComment}\n\n`; | ||
|
||
globalRequireOutputContent += getV3PackageRequireCode(getClientNamesSortedByPackageName(), { | ||
extraNewLine: true, | ||
}); | ||
globalRequireOutputContent += getV3ClientsNewExpressionCode(); | ||
|
||
return globalRequireOutputContent; | ||
}; |
14 changes: 14 additions & 0 deletions
14
scripts/generateNewClientTests/getServiceImportInputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { CLIENT_NAMES } from "../../src/transforms/v2-to-v3/utils/config"; | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getServiceImportInputContent = (codegenComment: string) => { | ||
let serviceImportInputContent = `${codegenComment}\n`; | ||
|
||
for (const clientName of CLIENT_NAMES) { | ||
serviceImportInputContent += `import ${clientName} from "aws-sdk/clients/${clientName.toLowerCase()}";\n`; | ||
} | ||
serviceImportInputContent += `\n`; | ||
serviceImportInputContent += getV2ClientsNewExpressionCode(); | ||
|
||
return serviceImportInputContent; | ||
}; |
13 changes: 13 additions & 0 deletions
13
scripts/generateNewClientTests/getServiceImportOutputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CLIENT_NAMES } from "../../src/transforms/v2-to-v3/utils/config"; | ||
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; | ||
import { getV3PackageImportsCode } from "./getV3PackageImportsCode"; | ||
|
||
export const getServiceImportOutputContent = (codegenComment: string) => { | ||
let serviceImportOutputContent = `${codegenComment}\n`; | ||
|
||
serviceImportOutputContent += getV3PackageImportsCode(CLIENT_NAMES); | ||
serviceImportOutputContent += `\n`; | ||
serviceImportOutputContent += getV3ClientsNewExpressionCode(); | ||
|
||
return serviceImportOutputContent; | ||
}; |
14 changes: 14 additions & 0 deletions
14
scripts/generateNewClientTests/getServiceRequireInputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { CLIENT_NAMES } from "../../src/transforms/v2-to-v3/utils/config"; | ||
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode"; | ||
|
||
export const getServiceRequireInputContent = (codegenComment: string) => { | ||
let serviceRequireInputContent = `${codegenComment}\n`; | ||
|
||
for (const clientName of CLIENT_NAMES) { | ||
serviceRequireInputContent += `const ${clientName} = require("aws-sdk/clients/${clientName.toLowerCase()}");\n`; | ||
} | ||
serviceRequireInputContent += `\n`; | ||
serviceRequireInputContent += getV2ClientsNewExpressionCode(); | ||
|
||
return serviceRequireInputContent; | ||
}; |
13 changes: 13 additions & 0 deletions
13
scripts/generateNewClientTests/getServiceRequireOutputContent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CLIENT_NAMES } from "../../src/transforms/v2-to-v3/utils/config"; | ||
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; | ||
import { getV3PackageRequireCode } from "./getV3PackageRequireCode"; | ||
|
||
export const getServiceRequireOutputContent = (codegenComment: string) => { | ||
let serviceRequireOutputContent = `${codegenComment}\n`; | ||
|
||
serviceRequireOutputContent += getV3PackageRequireCode(CLIENT_NAMES); | ||
serviceRequireOutputContent += `\n`; | ||
serviceRequireOutputContent += getV3ClientsNewExpressionCode(); | ||
|
||
return serviceRequireOutputContent; | ||
}; |
9 changes: 9 additions & 0 deletions
9
scripts/generateNewClientTests/getV2ClientsNewExpressionCode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { CLIENT_NAMES } from "../../src/transforms/v2-to-v3/utils/config"; | ||
|
||
export const getV2ClientsNewExpressionCode = (prefix?: string) => { | ||
let v2ClientsNewExpressionCode = ``; | ||
for (const clientName of CLIENT_NAMES) { | ||
v2ClientsNewExpressionCode += `new ${prefix || ""}${clientName}();\n`; | ||
} | ||
return v2ClientsNewExpressionCode; | ||
}; |
9 changes: 9 additions & 0 deletions
9
scripts/generateNewClientTests/getV3ClientsNewExpressionCode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { CLIENT_NAMES, CLIENT_NAMES_MAP } from "../../src/transforms/v2-to-v3/utils/config"; | ||
|
||
export const getV3ClientsNewExpressionCode = () => { | ||
let v3ClientsNewExpressionCode = ``; | ||
for (const v2ClientName of CLIENT_NAMES) { | ||
v3ClientsNewExpressionCode += `new ${CLIENT_NAMES_MAP[v2ClientName]}();\n`; | ||
} | ||
return v3ClientsNewExpressionCode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { | ||
CLIENT_NAMES, | ||
CLIENT_NAMES_MAP, | ||
CLIENT_PACKAGE_NAMES_MAP, | ||
} from "../../src/transforms/v2-to-v3/utils/config"; | ||
|
||
export const getV3PackageImportsCode = (sortedV2ClientNames: typeof CLIENT_NAMES) => { | ||
let v3PackageImportsCode = ``; | ||
for (const v2ClientName of sortedV2ClientNames) { | ||
const v3ClientName = CLIENT_NAMES_MAP[v2ClientName]; | ||
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`; | ||
v3PackageImportsCode += `import { ${v3ClientName} } from "${v3ClientPackageName}";\n`; | ||
} | ||
return v3PackageImportsCode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { | ||
CLIENT_NAMES, | ||
CLIENT_NAMES_MAP, | ||
CLIENT_PACKAGE_NAMES_MAP, | ||
} from "../../src/transforms/v2-to-v3/utils/config"; | ||
|
||
export const getV3PackageRequireCode = ( | ||
sortedV2ClientNames: typeof CLIENT_NAMES, | ||
{ extraNewLine = false }: { extraNewLine?: boolean } = {} | ||
) => { | ||
let v3PackageRequireCode = ``; | ||
for (const v2ClientName of sortedV2ClientNames) { | ||
const v3ClientName = CLIENT_NAMES_MAP[v2ClientName]; | ||
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`; | ||
v3PackageRequireCode += `const {\n ${v3ClientName}\n} = require("${v3ClientPackageName}");\n`; | ||
if (extraNewLine) v3PackageRequireCode += `\n`; | ||
} | ||
return v3PackageRequireCode; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// This script generates tests in src/transforms/v2-to-v3/__fixtures__/new-client | ||
// Run them using: yarn tsx scripts/generateNewClientTests/index.ts | ||
import { writeFile } from "fs/promises"; | ||
import { join } from "path"; | ||
|
||
import { getGlobalImportInputContent } from "./getGlobalImportInputContent"; | ||
import { getGlobalImportOutputContent } from "./getGlobalImportOutputContent"; | ||
import { getGlobalRequireInputContent } from "./getGlobalRequireInputContent"; | ||
import { getGlobalRequireOutputContent } from "./getGlobalRequireOutputContent"; | ||
import { getServiceImportInputContent } from "./getServiceImportInputContent"; | ||
import { getServiceImportOutputContent } from "./getServiceImportOutputContent"; | ||
import { getServiceRequireInputContent } from "./getServiceRequireInputContent"; | ||
import { getServiceRequireOutputContent } from "./getServiceRequireOutputContent"; | ||
|
||
// The "use strict" directive is added to so that comments can be attached to it. | ||
// Recast removes the comments while removing import/require. | ||
// Details in https://github.com/awslabs/aws-sdk-js-codemod/issues/205 | ||
const codegenComment = `// This file is generated by scripts/generateNewClientTests/index.ts | ||
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file. | ||
"use strict";`; | ||
|
||
const newClientsTestsFolder = join("src", "transforms", "v2-to-v3", "__fixtures__", "new-client"); | ||
const newClientTestsPath = join(__dirname, "..", "..", newClientsTestsFolder); | ||
|
||
(async () => { | ||
for (const [fileName, getFileContent] of [ | ||
["global-import.input.js", getGlobalImportInputContent], | ||
["global-import.output.js", getGlobalImportOutputContent], | ||
["global-require.input.js", getGlobalRequireInputContent], | ||
["global-require.output.js", getGlobalRequireOutputContent], | ||
["service-import.input.js", getServiceImportInputContent], | ||
["service-import.output.js", getServiceImportOutputContent], | ||
["service-require.input.js", getServiceRequireInputContent], | ||
["service-require.output.js", getServiceRequireOutputContent], | ||
] as [string, (comment: string) => string][]) { | ||
const filePath = join(newClientTestsPath, fileName); | ||
await writeFile(filePath, getFileContent(codegenComment)); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.