Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tweaking relative path handling for windows compatibility #18

Merged
merged 11 commits into from
Dec 14, 2023
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ on:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved
node-version: ['18']
steps:
- uses: actions/checkout@v4
Expand Down
128 changes: 122 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"src"
],
"scripts": {
"compile": "rimraf out && tsc -p ./ && cp -R algosdk out/algosdk",
"compile": "shx rm -rf out && tsc -p ./ && shx cp -R algosdk out/algosdk",
"lint": "eslint src --ext ts",
"typecheck": "tsc -p tsconfig.json --noEmit",
"check-format": "prettier . --check",
Expand All @@ -63,7 +63,7 @@
"extension:package": "vsce package",
"extension:publish": "vsce publish",
"extension:publish-pre-release": "vsce publish --pre-release",
"test": "ts-mocha -p tsconfig.json tests/*test.ts --timeout 30s --diff false",
"test": "ts-mocha -p tsconfig.json 'tests/**/*test.ts' --timeout 30s --diff false",
"test:coverage": "nyc npm run test"
},
"dependencies": {
Expand Down Expand Up @@ -97,7 +97,7 @@
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"prettier": "^3.0.3",
"rimraf": "^3.0.2",
"shx": "^0.3.4",
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved
"ts-mocha": "^10.0.0",
"typescript": "^4.6.3",
"url": "^0.11.3"
Expand Down
16 changes: 14 additions & 2 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,20 @@ export class ByteArrayMap<T> {
}
}

function filePathRelativeTo(base: string, filePath: string): string {
return new URL(filePath, new URL(base, 'file://')).pathname;
export function filePathRelativeTo(base: string, filePath: string): string {
// Normalize the base path to convert any Windows backslashes to forward slashes
// This is necessary because the URL object expects forward slashes
const normalizedBase = base.replace(/\\/g, '/');

// Create a URL object with the file protocol and the normalized base path
const baseURL = new URL(normalizedBase, 'file:///');
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved

// Resolve the file path against the base URL
const fullURL = new URL(filePath, baseURL);

// Convert the URL back to a local file path
// On Windows, this will correctly handle the drive letter and convert to backslashes
aorumbayev marked this conversation as resolved.
Show resolved Hide resolved
return fullURL.pathname;
}

interface ProgramSourceEntryFile {
Expand Down
31 changes: 31 additions & 0 deletions tests/common/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as assert from 'assert';
import { filePathRelativeTo } from '../../src/common/utils';

describe('Utils Tests', () => {
describe('filePathRelativeTo', () => {
// Given a valid base path and file path, it should return the correct relative file path.
it('should return the correct relative file path when arguments are unix like', () => {
const base = '/somelongpath/sources/sources.avm.json';
const filePath = 'slot-machine/slot-machine.teal.tok.map';

const result = filePathRelativeTo(base, filePath);

assert.strictEqual(
result,
'/somelongpath/sources/slot-machine/slot-machine.teal.tok.map',
);
});

it('should return the correct relative file path when arguments is windows like', () => {
const base = '\\somelongpath\\sources\\sources.avm.json';
const filePath = 'slot-machine/slot-machine.teal.tok.map';

const result = filePathRelativeTo(base, filePath);

assert.strictEqual(
result,
'/somelongpath/sources/slot-machine/slot-machine.teal.tok.map',
);
});
});
});
Loading