Skip to content

Commit

Permalink
fix: path normalizer (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanpas authored and JohnstonCode committed Feb 12, 2019
1 parent cbe9137 commit db214dd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/test/**/*.js"]
"outFiles": ["${workspaceRoot}/out/test/**/*.js"],
"preLaunchTask": "build"
}
]
}
25 changes: 15 additions & 10 deletions src/pathNormalizer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as path from "path";
import { posix as path } from "path";
import * as nativepath from "path";
import { Uri } from "vscode";
import { ISvnInfo } from "./common/types";
import { memoize } from "./decorators";
import { SvnRI } from "./svnRI";
import { pathOrRoot, SvnRI } from "./svnRI";

enum ResourceKind {
export enum ResourceKind {
LocalRelative,
LocalFull,
RemoteFull
Expand Down Expand Up @@ -32,7 +33,7 @@ export class PathNormalizer {
return fpath.substr(1);
} else if (fpath.startsWith("svn://") || fpath.startsWith("file://")) {
const target = Uri.parse(fpath).path;
return path.relative(this.repoRoot.path, target);
return path.relative(pathOrRoot(this.repoRoot), target);
} else {
throw new Error("unknown path");
}
Expand All @@ -53,10 +54,8 @@ export class PathNormalizer {
if (this.checkoutRoot === undefined) {
throw new Error("Local paths are not");
}
target = path.join(
this.fromRootToBranch(),
path.relative(this.checkoutRoot.path, fpath)
);
target = nativepath.relative(this.checkoutRoot.fsPath, fpath);
target = path.join(this.fromRootToBranch(), target);
} else if (kind === ResourceKind.LocalRelative) {
if (path.isAbsolute(fpath)) {
throw new Error("Path is absolute");
Expand All @@ -80,11 +79,17 @@ export class PathNormalizer {

@memoize
public fromRootToBranch(): string {
return path.relative(this.repoRoot.path, this.branchRoot.path);
return path.relative(
pathOrRoot(this.repoRoot),
pathOrRoot(this.branchRoot)
);
}

@memoize
public fromBranchToRoot(): string {
return path.relative(this.branchRoot.path, this.repoRoot.path);
return path.relative(
pathOrRoot(this.branchRoot),
pathOrRoot(this.repoRoot)
);
}
}
11 changes: 9 additions & 2 deletions src/svnRI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as path from "path";
import { posix as path } from "path";
import { Uri } from "vscode";
import { memoize } from "./decorators";

export function pathOrRoot(uri: Uri): string {
return uri.path || "/";
}

export class SvnRI {
constructor(
private readonly remoteRoot: Uri,
Expand Down Expand Up @@ -41,7 +45,10 @@ export class SvnRI {

@memoize
get fromRepoToBranch(): string {
return path.relative(this.remoteRoot.path, this.branchRoot.path);
return path.relative(
pathOrRoot(this.remoteRoot),
pathOrRoot(this.branchRoot)
);
}

@memoize
Expand Down
74 changes: 71 additions & 3 deletions src/test/pathNormalizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
// The module 'assert' provides assertion methods from node
import * as assert from "assert";
import * as path from "path";
import { PathNormalizer } from "../pathNormalizer";
import { PathNormalizer, ResourceKind } from "../pathNormalizer";
import { Uri } from "vscode";
import { ISvnInfo } from "../common/types";

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

test("r1 ops", function() {
test("somedomain", function() {
assert.equal(nm1.branchRoot.toString(), Uri.parse(ri1.url).toString());
assert.equal(
nm1.repoRoot.toString(),
Expand All @@ -52,5 +52,73 @@ suite("Url parsing", () => {
throw new Error("impossible");
}
assert.equal(x1.localFullPath.toString(), "file:///home/d1/f1");
const x2 = nm1.parse("/branches/features/F1/dir/file.c");
assert.equal(
x2.localFullPath!.toString(),
"file:///home/user/dev/mypero/dir/file.c"
);
});

const ri2 = {
repository: {
root: "svn://rootdomain.com"
},
url: "svn://rootdomain.com/foo/drupal-7/trunk",
wcInfo: {
wcrootAbspath: "/home/dev-mi/projects/drupal/foo"
}
};
const nm2 = new PathNormalizer(ri2 as ISvnInfo);

test("rootdomain", function() {
const p1 = nm2.parse(
"/foo/drupal-7/trunk/drupal/sites/all/themes/foo_theme/scss/foo-pdf.scss"
);
assert.equal(
p1.localFullPath!.path,
"/home/dev-mi/projects/drupal/foo/drupal/sites/all/themes/foo_theme/scss/foo-pdf.scss"
);

const p2 = nm2.parse("drupal/sites", ResourceKind.LocalRelative);
assert.equal(p2.remoteFullPath.path, "/foo/drupal-7/trunk/drupal/sites");
const p3 = nm2.parse(
"/home/dev-mi/projects/drupal/foo/drupal",
ResourceKind.LocalFull
);
assert.equal(p3.remoteFullPath.path, "/foo/drupal-7/trunk/drupal");
});

const ri3 = {
repository: {
root: "svn://rootdomain.com"
},
url: "svn://rootdomain.com",
wcInfo: {
wcrootAbspath: "/home/user/svn"
}
};
const nm3 = new PathNormalizer(ri3 as ISvnInfo);

test("rootbranch", function() {
const p4 = nm3.parse("/file.c");
assert.equal(p4.localFullPath!.path, "/home/user/svn/file.c");
});

const ri4 = {
repository: {
root: "svn://rootdomain.com"
},
url: "svn://rootdomain.com/trunk",
wcInfo: {
wcrootAbspath: "X:\\work\\rootd"
}
};
const nm4 = new PathNormalizer(ri4 as ISvnInfo);

if (process.platform == "win32") {
test("winpath", function() {
const p4 = nm4.parse("/trunk/file.c");
assert.equal(p4.localFullPath!.fsPath, "x:\\work\\rootd\\file.c");
});
}
});

0 comments on commit db214dd

Please sign in to comment.