|
| 1 | +import { expect } from "chai" |
| 2 | +import sinon from "sinon" |
| 3 | +import { DataSource, DataSourceOptions } from "../../../src" |
| 4 | +import { |
| 5 | + closeTestingConnections, |
| 6 | + createTestingConnections, |
| 7 | +} from "../../utils/test-utils" |
| 8 | +import { CommandUtils } from "../../../src/commands/CommandUtils" |
| 9 | +import { MigrationGenerateCommand } from "../../../src/commands/MigrationGenerateCommand" |
| 10 | +import { Post } from "./entity/Post" |
| 11 | + |
| 12 | +describe("commands - migration generate - template literal escaping", () => { |
| 13 | + let dataSources: DataSource[] |
| 14 | + let createFileStub: sinon.SinonStub |
| 15 | + let loadDataSourceStub: sinon.SinonStub |
| 16 | + let migrationGenerateCommand: MigrationGenerateCommand |
| 17 | + |
| 18 | + before(async () => { |
| 19 | + dataSources = await createTestingConnections({ |
| 20 | + entities: [Post], |
| 21 | + enabledDrivers: ["postgres", "mysql", "mariadb", "cockroachdb"], |
| 22 | + schemaCreate: true, |
| 23 | + dropSchema: true, |
| 24 | + }) |
| 25 | + migrationGenerateCommand = new MigrationGenerateCommand() |
| 26 | + createFileStub = sinon.stub(CommandUtils, "createFile") |
| 27 | + loadDataSourceStub = sinon.stub(CommandUtils, "loadDataSource") |
| 28 | + }) |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + createFileStub.resetHistory() |
| 32 | + loadDataSourceStub.resetHistory() |
| 33 | + }) |
| 34 | + |
| 35 | + after(async () => { |
| 36 | + createFileStub.restore() |
| 37 | + loadDataSourceStub.restore() |
| 38 | + await closeTestingConnections(dataSources) |
| 39 | + }) |
| 40 | + |
| 41 | + async function generateWithComment( |
| 42 | + dataSource: DataSource, |
| 43 | + comment: string, |
| 44 | + timestamp: string, |
| 45 | + ): Promise<string | undefined> { |
| 46 | + const driver = dataSource.driver.options.type |
| 47 | + |
| 48 | + if (driver === "postgres" || driver === "cockroachdb") { |
| 49 | + await dataSource.query( |
| 50 | + `COMMENT ON COLUMN "post"."title" IS '${comment}'`, |
| 51 | + ) |
| 52 | + } else { |
| 53 | + const sqlComment = comment |
| 54 | + .replace(/\\/g, "\\\\") |
| 55 | + .replace(/'/g, "\\'") |
| 56 | + await dataSource.query( |
| 57 | + `ALTER TABLE \`post\` MODIFY COLUMN \`title\` varchar(255) NOT NULL COMMENT '${sqlComment}'`, |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + const freshDataSource = new DataSource( |
| 62 | + dataSource.options as DataSourceOptions, |
| 63 | + ) |
| 64 | + loadDataSourceStub.resolves(freshDataSource) |
| 65 | + |
| 66 | + await migrationGenerateCommand.handler({ |
| 67 | + $0: "test", |
| 68 | + _: ["test"], |
| 69 | + path: "test-directory/test-migration", |
| 70 | + dataSource: "dummy-path", |
| 71 | + timestamp, |
| 72 | + exitProcess: false, |
| 73 | + }) |
| 74 | + |
| 75 | + const fileContent = createFileStub.firstCall?.args[1] as |
| 76 | + | string |
| 77 | + | undefined |
| 78 | + createFileStub.resetHistory() |
| 79 | + |
| 80 | + if (driver === "postgres" || driver === "cockroachdb") { |
| 81 | + await dataSource.query(`COMMENT ON COLUMN "post"."title" IS NULL`) |
| 82 | + } else { |
| 83 | + await dataSource.query( |
| 84 | + `ALTER TABLE \`post\` MODIFY COLUMN \`title\` varchar(255) NOT NULL`, |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + return fileContent |
| 89 | + } |
| 90 | + |
| 91 | + const cases: [string, string, (content: string) => void][] = [ |
| 92 | + [ |
| 93 | + "backticks", |
| 94 | + "contains `backticks` in comment", |
| 95 | + (content) => { |
| 96 | + expect(content).to.not.match(/[^\\]`contains/) |
| 97 | + expect(content).to.contain("\\`") |
| 98 | + }, |
| 99 | + ], |
| 100 | + [ |
| 101 | + "backslashes", |
| 102 | + "path\\to\\file", |
| 103 | + (content) => { |
| 104 | + expect(content).to.contain("\\\\") |
| 105 | + }, |
| 106 | + ], |
| 107 | + [ |
| 108 | + "template literal interpolation", |
| 109 | + "${process.exit(1)}", |
| 110 | + (content) => { |
| 111 | + expect(content).to.not.match(/(^|[^\\])\$\{process/) |
| 112 | + expect(content).to.contain("\\${") |
| 113 | + }, |
| 114 | + ], |
| 115 | + [ |
| 116 | + "backslash before interpolation", |
| 117 | + "\\${process.exit(1)}", |
| 118 | + (content) => { |
| 119 | + expect(content).to.not.match(/[^\\]\$\{process/) |
| 120 | + }, |
| 121 | + ], |
| 122 | + ] |
| 123 | + |
| 124 | + for (const [label, comment, assertion] of cases) { |
| 125 | + it(`should escape ${label} in column comments`, async () => { |
| 126 | + for (const dataSource of dataSources) { |
| 127 | + const content = await generateWithComment( |
| 128 | + dataSource, |
| 129 | + comment, |
| 130 | + "9999999999999", |
| 131 | + ) |
| 132 | + expect(content).to.not.be.undefined |
| 133 | + assertion(content!) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | +}) |
0 commit comments