nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: Redocly/openapi-cli
Pull request #240 (2021-01-09 18:00:57)

fix: fix push uploads to the wrong path on windows


Patch:
diff --git a/packages/cli/src/commands/push.ts b/packages/cli/src/commands/push.ts
index 680f308c..614f3469 100644
--- a/packages/cli/src/commands/push.ts
+++ b/packages/cli/src/commands/push.ts
@@ -4,6 +4,7 @@ import fetch from 'node-fetch';
 import { performance } from 'perf_hooks';
 import { yellow, green, blue } from 'colorette';
 import { createHash } from 'crypto';
+
 import { bundle, Config, loadConfig, RedoclyClient, IGNORE_FILE, BundleOutputFormat } from '@redocly/openapi-core';
 import {
   promptUser,
@@ -13,6 +14,7 @@ import {
   getTotals,
   pluralize,
   dumpBundle,
+  slash
 } from '../utils';
 
 type Source = {
@@ -186,8 +188,8 @@ async function collectFilesToUpload(entrypoint: string) {
     return {
       filePath: path.resolve(filename),
       keyOnS3: config.configFile
-        ? path.relative(path.dirname(config.configFile), filename)
-        : path.basename(filename),
+        ? slash(path.relative(path.dirname(config.configFile), filename))
+        : slash(path.basename(filename)),
       contents: contents && Buffer.from(contents, 'utf-8') || undefined,
     }
   }
@@ -223,4 +225,4 @@ function uploadFileToS3(url: string, filePathOrBuffer: string | Buffer) {
     },
     body: readStream
   })
-}
+}
\ No newline at end of file
diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts
index 498f2c30..eafb2505 100644
--- a/packages/cli/src/utils.ts
+++ b/packages/cli/src/utils.ts
@@ -281,3 +281,16 @@ export function exitWithError(message: string) {
   process.stderr.write(red(message)+ '\n\n');
   process.exit(1);
 }
+
+
+/**
+ * Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
+ */
+export function slash(path: string): string {
+  const isExtendedLengthPath = /^\\\\\?\\/.test(path)
+  if (isExtendedLengthPath) {
+    return path
+  }
+
+  return path.replace(/\\/g, '/');
+}
\ No newline at end of file


Test patch:
diff --git a/packages/cli/src/__tests__/utils.test.ts b/packages/cli/src/__tests__/utils.test.ts
new file mode 100644
index 00000000..508185c5
--- /dev/null
+++ b/packages/cli/src/__tests__/utils.test.ts
@@ -0,0 +1,19 @@
+import { slash } from '../utils';
+
+describe('slash path', () => {
+  it('can correctly slash path', () => {
+    [
+      ['foo\\bar', 'foo/bar'],
+      ['foo/bar', 'foo/bar'],
+      ['foo\\中文', 'foo/中文'],
+      ['foo/中文', 'foo/中文'],
+    ].forEach(([path, expectRes]) => {
+      expect(slash(path)).toBe(expectRes);
+    });
+  });
+
+  it('does not modify extended length paths', () => {
+    const extended = '\\\\?\\some\\path';
+    expect(slash(extended)).toBe(extended);
+  });
+});