Repository: stateful/vscode-runme
Pull request #307 (2023-03-15 21:58:49)

Disable grpc runner command normalization
Along with stateful/runme#189, this improves the command normalization situation for the gRPC runner.

sourishkrout: @mxsdev any particular reason why this wasn't merged?

Patch:
diff --git a/src/extension/executors/runner.ts b/src/extension/executors/runner.ts
index 7ce4813e..9c2a3701 100644
--- a/src/extension/executors/runner.ts
+++ b/src/extension/executors/runner.ts
@@ -57,8 +57,8 @@ export async function executeRunner(
     RUNME_ID
   }
 
-  const commands = await parseCommandSeq(cellText)
-  if (!commands) { return false }
+  const commands = await parseCommandSeq(cellText, (s) => s.split('\n'))
+  if(!commands) { return false }
 
   if (commands.length === 0) {
     commands.push('')
diff --git a/src/extension/executors/utils.ts b/src/extension/executors/utils.ts
index a58dfc73..5c58d8fd 100644
--- a/src/extension/executors/utils.ts
+++ b/src/extension/executors/utils.ts
@@ -7,7 +7,7 @@ import { NotebookCellOutput, NotebookCellExecution, NotebookCellOutputItem, wind
 import { ENV_STORE } from '../constants'
 import { OutputType } from '../../constants'
 import type { CellOutputPayload } from '../../types'
-import { replaceOutput, getCmdSeq } from '../utils'
+import { replaceOutput } from '../utils'
 
 const ENV_VAR_REGEXP = /(\$\w+)/g
 /**
@@ -60,7 +60,8 @@ export async function promptUserForVariable(
 }
 
 export function getCommandExportExtractMatches(
-  rawText: string
+  rawText: string,
+  supportsDirect = true
 ): CommandExportExtractMatch[] {
   const test = new RegExp(EXPORT_EXTRACT_REGEX)
 
@@ -85,8 +86,10 @@ export function getCommandExportExtractMatches(
       value = placeHolder.slice(2, -1)
     } else if (!placeHolder.includes('\n')) {
       matchType = 'prompt'
-    } else {
+    } else if (supportsDirect) {
       matchType = 'direct'
+    } else {
+      continue
     }
 
     result.push({
@@ -191,9 +194,12 @@ export function getShellPath(execKey?: string): string|undefined {
  * Returns `undefined` when a user cancels on prompt
  */
 export async function parseCommandSeq(
-  cellText: string
+  cellText: string,
+  parseBlock?: (block: string) => string[]
 ): Promise<string[]|undefined> {
-  const exportMatches = getCommandExportExtractMatches(cellText)
+  parseBlock ??= (s) => s ? s.split('\n') : []
+
+  const exportMatches = getCommandExportExtractMatches(cellText, false)
 
   type CommandBlock =
     |{
@@ -243,7 +249,7 @@ export async function parseCommandSeq(
   parsedCommandBlocks.push({ type: 'block', content: cellText.slice(offset) })
 
   return parsedCommandBlocks
-    .flatMap(({ type, content }) => type === 'block' ? getCmdSeq(content) : [content])
+    .flatMap(({ type, content }) => (type === 'block' && parseBlock?.(content)) || (content ? [content] : []))
 }
 
 export function isWindows(): boolean {


Test patch:
diff --git a/tests/extension/executors/utils.test.ts b/tests/extension/executors/utils.test.ts
index 189744d7..3752f473 100644
--- a/tests/extension/executors/utils.test.ts
+++ b/tests/extension/executors/utils.test.ts
@@ -5,6 +5,7 @@ import { expect, vi, test, suite, beforeEach } from 'vitest'
 
 import { ENV_STORE } from '../../../src/extension/constants'
 import { retrieveShellCommand, parseCommandSeq } from '../../../src/extension/executors/utils'
+import { getCmdSeq } from '../../../src/extension/utils'
 
 const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
 
@@ -208,25 +209,71 @@ suite('parseCommandSeq', () => {
     expect(res?.[0]).toBe('export TEST="test value"')
   })
 
+  test('single line export with cancelled prompt', async () => {
+    vi.mocked(window.showInputBox).mockImplementationOnce(async () => undefined)
+
+    const res = await parseCommandSeq([
+      'export TEST="<placeholder>"'
+    ].join('\n'))
+
+    expect(res).toBe(undefined)
+  })
+
   test('multiline export', async () => {
-    const exportLine = [
+    const exportLines = [
       'export TEST="I',
       'am',
       'doing',
       'well!"'
-    ].join('\n')
+    ]
 
-    const res = await parseCommandSeq(exportLine)
+    const res = await parseCommandSeq(exportLines.join('\n'))
 
     expect(res).toBeTruthy
-    expect(res).toHaveLength(1)
-    expect(res?.[0]).toBe(exportLine)
+    expect(res).toHaveLength(4)
+    expect(res).toStrictEqual(exportLines)
   })
 
   test('exports between normal command sequences', async () => {
     vi.mocked(window.showInputBox).mockImplementationOnce(async () => 'test value')
 
-    const cmd = [
+    const cmdLines = [
+      'echo "Hello!"',
+      'echo "Hi!"',
+      'export TEST="<placeholder>"',
+      'echo $TEST',
+      'export TEST_MULTILINE="This',
+      'is',
+      'a',
+      'multiline',
+      'env!"',
+      'echo $TEST_MULTILINE'
+    ]
+
+    const res = await parseCommandSeq(cmdLines.join('\n'))
+
+    expect(res).toBeTruthy()
+    expect(res).toStrictEqual([
+      'echo "Hello!"',
+      'echo "Hi!"',
+      'export TEST="test value"',
+      '',
+      'echo $TEST',
+      ...[
+        'export TEST_MULTILINE="This',
+        'is',
+        'a',
+        'multiline',
+        'env!"',
+      ],
+      'echo $TEST_MULTILINE'
+    ])
+  })
+
+  test('exports between normal command sequences with getCmdSeq', async () => {
+    vi.mocked(window.showInputBox).mockImplementationOnce(async () => 'test value')
+
+    const cmdLines = [
       'echo "Hello!"',
       'echo "Hi!"',
       'export TEST="<placeholder>"',
@@ -237,9 +284,9 @@ suite('parseCommandSeq', () => {
       'multiline',
       'env!"',
       'echo $TEST_MULTILINE'
-    ].join('\n')
+    ]
 
-    const res = await parseCommandSeq(cmd)
+    const res = await parseCommandSeq(cmdLines.join('\n'), getCmdSeq)
 
     expect(res).toBeTruthy()
     expect(res).toStrictEqual([
@@ -247,13 +294,13 @@ suite('parseCommandSeq', () => {
       'echo "Hi!"',
       'export TEST="test value"',
       'echo $TEST',
-      [
+      ...[
         'export TEST_MULTILINE="This',
         'is',
         'a',
         'multiline',
         'env!"',
-      ].join('\n'),
+      ],
       'echo $TEST_MULTILINE'
     ])
   })