Skip to content

Commit db214dd

Browse files
YanpasJohnstonCode
authored andcommitted
fix: path normalizer (#477)
1 parent cbe9137 commit db214dd

File tree

4 files changed

+97
-16
lines changed

4 files changed

+97
-16
lines changed

.vscode/launch.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
],
2525
"stopOnEntry": false,
2626
"sourceMaps": true,
27-
"outFiles": ["${workspaceRoot}/out/test/**/*.js"]
27+
"outFiles": ["${workspaceRoot}/out/test/**/*.js"],
28+
"preLaunchTask": "build"
2829
}
2930
]
3031
}

src/pathNormalizer.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import * as path from "path";
1+
import { posix as path } from "path";
2+
import * as nativepath from "path";
23
import { Uri } from "vscode";
34
import { ISvnInfo } from "./common/types";
45
import { memoize } from "./decorators";
5-
import { SvnRI } from "./svnRI";
6+
import { pathOrRoot, SvnRI } from "./svnRI";
67

7-
enum ResourceKind {
8+
export enum ResourceKind {
89
LocalRelative,
910
LocalFull,
1011
RemoteFull
@@ -32,7 +33,7 @@ export class PathNormalizer {
3233
return fpath.substr(1);
3334
} else if (fpath.startsWith("svn://") || fpath.startsWith("file://")) {
3435
const target = Uri.parse(fpath).path;
35-
return path.relative(this.repoRoot.path, target);
36+
return path.relative(pathOrRoot(this.repoRoot), target);
3637
} else {
3738
throw new Error("unknown path");
3839
}
@@ -53,10 +54,8 @@ export class PathNormalizer {
5354
if (this.checkoutRoot === undefined) {
5455
throw new Error("Local paths are not");
5556
}
56-
target = path.join(
57-
this.fromRootToBranch(),
58-
path.relative(this.checkoutRoot.path, fpath)
59-
);
57+
target = nativepath.relative(this.checkoutRoot.fsPath, fpath);
58+
target = path.join(this.fromRootToBranch(), target);
6059
} else if (kind === ResourceKind.LocalRelative) {
6160
if (path.isAbsolute(fpath)) {
6261
throw new Error("Path is absolute");
@@ -80,11 +79,17 @@ export class PathNormalizer {
8079

8180
@memoize
8281
public fromRootToBranch(): string {
83-
return path.relative(this.repoRoot.path, this.branchRoot.path);
82+
return path.relative(
83+
pathOrRoot(this.repoRoot),
84+
pathOrRoot(this.branchRoot)
85+
);
8486
}
8587

8688
@memoize
8789
public fromBranchToRoot(): string {
88-
return path.relative(this.branchRoot.path, this.repoRoot.path);
90+
return path.relative(
91+
pathOrRoot(this.branchRoot),
92+
pathOrRoot(this.repoRoot)
93+
);
8994
}
9095
}

src/svnRI.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import * as path from "path";
1+
import { posix as path } from "path";
22
import { Uri } from "vscode";
33
import { memoize } from "./decorators";
44

5+
export function pathOrRoot(uri: Uri): string {
6+
return uri.path || "/";
7+
}
8+
59
export class SvnRI {
610
constructor(
711
private readonly remoteRoot: Uri,
@@ -41,7 +45,10 @@ export class SvnRI {
4145

4246
@memoize
4347
get fromRepoToBranch(): string {
44-
return path.relative(this.remoteRoot.path, this.branchRoot.path);
48+
return path.relative(
49+
pathOrRoot(this.remoteRoot),
50+
pathOrRoot(this.branchRoot)
51+
);
4552
}
4653

4754
@memoize

src/test/pathNormalizer.test.ts

+71-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// The module 'assert' provides assertion methods from node
99
import * as assert from "assert";
1010
import * as path from "path";
11-
import { PathNormalizer } from "../pathNormalizer";
11+
import { PathNormalizer, ResourceKind } from "../pathNormalizer";
1212
import { Uri } from "vscode";
1313
import { ISvnInfo } from "../common/types";
1414

1515
// Defines a Mocha test suite to group tests of similar kind together
16-
suite("Url parsing", () => {
16+
suite("SVN URLs parsing", () => {
1717
const ri1 = {
1818
repository: {
1919
root: "svn://somedomain.x.org/public/devs"
@@ -33,7 +33,7 @@ suite("Url parsing", () => {
3333
// do nothing
3434
});
3535

36-
test("r1 ops", function() {
36+
test("somedomain", function() {
3737
assert.equal(nm1.branchRoot.toString(), Uri.parse(ri1.url).toString());
3838
assert.equal(
3939
nm1.repoRoot.toString(),
@@ -52,5 +52,73 @@ suite("Url parsing", () => {
5252
throw new Error("impossible");
5353
}
5454
assert.equal(x1.localFullPath.toString(), "file:///home/d1/f1");
55+
const x2 = nm1.parse("/branches/features/F1/dir/file.c");
56+
assert.equal(
57+
x2.localFullPath!.toString(),
58+
"file:///home/user/dev/mypero/dir/file.c"
59+
);
60+
});
61+
62+
const ri2 = {
63+
repository: {
64+
root: "svn://rootdomain.com"
65+
},
66+
url: "svn://rootdomain.com/foo/drupal-7/trunk",
67+
wcInfo: {
68+
wcrootAbspath: "/home/dev-mi/projects/drupal/foo"
69+
}
70+
};
71+
const nm2 = new PathNormalizer(ri2 as ISvnInfo);
72+
73+
test("rootdomain", function() {
74+
const p1 = nm2.parse(
75+
"/foo/drupal-7/trunk/drupal/sites/all/themes/foo_theme/scss/foo-pdf.scss"
76+
);
77+
assert.equal(
78+
p1.localFullPath!.path,
79+
"/home/dev-mi/projects/drupal/foo/drupal/sites/all/themes/foo_theme/scss/foo-pdf.scss"
80+
);
81+
82+
const p2 = nm2.parse("drupal/sites", ResourceKind.LocalRelative);
83+
assert.equal(p2.remoteFullPath.path, "/foo/drupal-7/trunk/drupal/sites");
84+
const p3 = nm2.parse(
85+
"/home/dev-mi/projects/drupal/foo/drupal",
86+
ResourceKind.LocalFull
87+
);
88+
assert.equal(p3.remoteFullPath.path, "/foo/drupal-7/trunk/drupal");
5589
});
90+
91+
const ri3 = {
92+
repository: {
93+
root: "svn://rootdomain.com"
94+
},
95+
url: "svn://rootdomain.com",
96+
wcInfo: {
97+
wcrootAbspath: "/home/user/svn"
98+
}
99+
};
100+
const nm3 = new PathNormalizer(ri3 as ISvnInfo);
101+
102+
test("rootbranch", function() {
103+
const p4 = nm3.parse("/file.c");
104+
assert.equal(p4.localFullPath!.path, "/home/user/svn/file.c");
105+
});
106+
107+
const ri4 = {
108+
repository: {
109+
root: "svn://rootdomain.com"
110+
},
111+
url: "svn://rootdomain.com/trunk",
112+
wcInfo: {
113+
wcrootAbspath: "X:\\work\\rootd"
114+
}
115+
};
116+
const nm4 = new PathNormalizer(ri4 as ISvnInfo);
117+
118+
if (process.platform == "win32") {
119+
test("winpath", function() {
120+
const p4 = nm4.parse("/trunk/file.c");
121+
assert.equal(p4.localFullPath!.fsPath, "x:\\work\\rootd\\file.c");
122+
});
123+
}
56124
});

0 commit comments

Comments
 (0)