Skip to content

Commit 9aa7b21

Browse files
committed
use proper TOML tooling instead of regex for fixture helpers
1 parent fa365b7 commit 9aa7b21

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

packages/e2e/data/valid-app/shopify.app.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Comprehensive shopify.app.toml for E2E regression testing
2-
# client_id is injected at runtime via injectFixtureToml()
3-
client_id = "__CLIENT_ID__"
4-
name = "__NAME__"
2+
# client_id and name are patched at runtime via injectFixtureToml()
3+
client_id = "placeholder"
4+
name = "placeholder"
55
application_url = "https://example.com"
66
embedded = true
77

packages/e2e/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
}
3030
},
3131
"devDependencies": {
32+
"@iarna/toml": "2.2.5",
3233
"@playwright/test": "^1.50.0",
34+
"@shopify/toml-patch": "0.3.0",
3335
"@types/node": "18.19.70",
3436
"dotenv": "16.4.7",
3537
"execa": "^7.2.0",

packages/e2e/setup/app.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import {authFixture} from './auth.js'
33
import {navigateToDashboard} from './browser.js'
44
import {CLI_TIMEOUT, BROWSER_TIMEOUT} from './constants.js'
5+
import {updateTomlValues} from '@shopify/toml-patch'
6+
import * as toml from '@iarna/toml'
57
import * as path from 'path'
68
import * as fs from 'fs'
79
import type {CLIContext, CLIProcess, ExecResult} from './cli.js'
@@ -84,23 +86,27 @@ export async function createApp(ctx: {
8486
* Read the client_id from a shopify.app.toml file.
8587
*/
8688
export function extractClientId(appDir: string): string {
87-
const toml = fs.readFileSync(path.join(appDir, 'shopify.app.toml'), 'utf8')
88-
const match = toml.match(/client_id\s*=\s*"([^"]+)"/)
89-
if (!match?.[1]) {
90-
throw new Error(`Could not find client_id in ${path.join(appDir, 'shopify.app.toml')}`)
89+
const tomlPath = path.join(appDir, 'shopify.app.toml')
90+
const parsed = toml.parse(fs.readFileSync(tomlPath, 'utf8'))
91+
const clientId = parsed.client_id as string | undefined
92+
if (!clientId) {
93+
throw new Error(`Could not find client_id in ${tomlPath}`)
9194
}
92-
return match[1]
95+
return clientId
9396
}
9497

9598
/**
9699
* Overwrite a created app's shopify.app.toml with a fixture TOML template.
97-
* The template should contain `__CLIENT_ID__` and `__NAME__` placeholders which get
98-
* replaced with the app's real client_id and the provided name.
100+
* Uses toml-patch to surgically set client_id and name while
101+
* preserving comments and formatting in the fixture file.
99102
*/
100103
export function injectFixtureToml(appDir: string, fixtureTomlContent: string, name: string): void {
101104
const clientId = extractClientId(appDir)
102-
const toml = fixtureTomlContent.replace(/__CLIENT_ID__/g, clientId).replace(/__NAME__/g, name)
103-
fs.writeFileSync(path.join(appDir, 'shopify.app.toml'), toml)
105+
const patched = updateTomlValues(fixtureTomlContent, [
106+
[['client_id'], clientId],
107+
[['name'], name],
108+
])
109+
fs.writeFileSync(path.join(appDir, 'shopify.app.toml'), patched)
104110
}
105111

106112
export async function generateExtension(

packages/e2e/tests/dev-hot-reload.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {appTestFixture as test, createApp, injectFixtureToml, teardownApp} from '../setup/app.js'
44
import {CLI_TIMEOUT, TEST_TIMEOUT} from '../setup/constants.js'
55
import {requireEnv} from '../setup/env.js'
6+
import {updateTomlValues} from '@shopify/toml-patch'
67
import {expect} from '@playwright/test'
78
import * as fs from 'fs'
89
import * as path from 'path'
@@ -60,13 +61,8 @@ test.describe('Dev hot reload', () => {
6061
// Edit scopes in the TOML to trigger a reload
6162
const tomlPath = path.join(appDir, 'shopify.app.toml')
6263
const original = fs.readFileSync(tomlPath, 'utf8')
63-
fs.writeFileSync(
64-
tomlPath,
65-
original.replace(
66-
'scopes = "read_products,write_products,read_orders"',
67-
'scopes = "read_products,write_products"',
68-
),
69-
)
64+
const patched = updateTomlValues(original, [[['access_scopes', 'scopes'], 'read_products,write_products']])
65+
fs.writeFileSync(tomlPath, patched)
7066

7167
await proc.waitForOutput('App config updated', CLI_TIMEOUT.medium)
7268
await proc.waitForOutput(UPDATED_MESSAGE, CLI_TIMEOUT.medium)

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)