summaryrefslogtreecommitdiffstats
path: root/toolkit/components/osfile/tests/xpcshell/test_path.js
blob: 76a507ee3855b46ff79b97c5d4013e16200663b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

Components.utils.import("resource://gre/modules/Services.jsm", this);
Services.prefs.setBoolPref("toolkit.osfile.test.syslib_necessary", false);
  // We don't need libc/kernel32.dll for this test

var ImportWin = {};
var ImportUnix = {};
Components.utils.import("resource://gre/modules/osfile/ospath_win.jsm", ImportWin);
Components.utils.import("resource://gre/modules/osfile/ospath_unix.jsm", ImportUnix);

var Win = ImportWin;
var Unix = ImportUnix;

function do_check_fail(f)
{
  try {
    let result = f();
    do_print("Failed do_check_fail: " + result);
    do_check_true(false);
  } catch (ex) {
    do_check_true(true);
  }
};

function run_test()
{
  do_print("Testing Windows paths");

  do_print("Backslash-separated, no drive");
  do_check_eq(Win.basename("a\\b"), "b");
  do_check_eq(Win.basename("a\\b\\"), "");
  do_check_eq(Win.basename("abc"), "abc");
  do_check_eq(Win.dirname("a\\b"), "a");
  do_check_eq(Win.dirname("a\\b\\"), "a\\b");
  do_check_eq(Win.dirname("a\\\\\\\\b"), "a");
  do_check_eq(Win.dirname("abc"), ".");
  do_check_eq(Win.normalize("\\a\\b\\c"), "\\a\\b\\c");
  do_check_eq(Win.normalize("\\a\\b\\\\\\\\c"), "\\a\\b\\c");
  do_check_eq(Win.normalize("\\a\\b\\c\\\\\\"), "\\a\\b\\c");
  do_check_eq(Win.normalize("\\a\\b\\c\\..\\..\\..\\d\\e\\f"), "\\d\\e\\f");
  do_check_eq(Win.normalize("a\\b\\c\\..\\..\\..\\d\\e\\f"), "d\\e\\f");
  do_check_fail(() => Win.normalize("\\a\\b\\c\\..\\..\\..\\..\\d\\e\\f"));

  do_check_eq(Win.join("\\tmp", "foo", "bar"), "\\tmp\\foo\\bar", "join \\tmp,foo,bar");
  do_check_eq(Win.join("\\tmp", "\\foo", "bar"), "\\foo\\bar", "join \\tmp,\\foo,bar");
  do_check_eq(Win.winGetDrive("\\tmp"), null);
  do_check_eq(Win.winGetDrive("\\tmp\\a\\b\\c\\d\\e"), null);
  do_check_eq(Win.winGetDrive("\\"), null);


  do_print("Backslash-separated, with a drive");
  do_check_eq(Win.basename("c:a\\b"), "b");
  do_check_eq(Win.basename("c:a\\b\\"), "");
  do_check_eq(Win.basename("c:abc"), "abc");
  do_check_eq(Win.dirname("c:a\\b"), "c:a");
  do_check_eq(Win.dirname("c:a\\b\\"), "c:a\\b");
  do_check_eq(Win.dirname("c:a\\\\\\\\b"), "c:a");
  do_check_eq(Win.dirname("c:abc"), "c:");
  let options = {
    winNoDrive: true
  };
  do_check_eq(Win.dirname("c:a\\b", options), "a");
  do_check_eq(Win.dirname("c:a\\b\\", options), "a\\b");
  do_check_eq(Win.dirname("c:a\\\\\\\\b", options), "a");
  do_check_eq(Win.dirname("c:abc", options), ".");
  do_check_eq(Win.join("c:", "abc"), "c:\\abc", "join c:,abc");

  do_check_eq(Win.normalize("c:"), "c:\\");
  do_check_eq(Win.normalize("c:\\"), "c:\\");
  do_check_eq(Win.normalize("c:\\a\\b\\c"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:\\a\\b\\\\\\\\c"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:\\\\\\\\a\\b\\c"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:\\a\\b\\c\\\\\\"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:\\a\\b\\c\\..\\..\\..\\d\\e\\f"), "c:\\d\\e\\f");
  do_check_eq(Win.normalize("c:a\\b\\c\\..\\..\\..\\d\\e\\f"), "c:\\d\\e\\f");
  do_check_fail(() => Win.normalize("c:\\a\\b\\c\\..\\..\\..\\..\\d\\e\\f"));

  do_check_eq(Win.join("c:\\", "foo"), "c:\\foo", "join c:\,foo");
  do_check_eq(Win.join("c:\\tmp", "foo", "bar"), "c:\\tmp\\foo\\bar", "join c:\\tmp,foo,bar");
  do_check_eq(Win.join("c:\\tmp", "\\foo", "bar"), "c:\\foo\\bar", "join c:\\tmp,\\foo,bar");
  do_check_eq(Win.join("c:\\tmp", "c:\\foo", "bar"), "c:\\foo\\bar", "join c:\\tmp,c:\\foo,bar");
  do_check_eq(Win.join("c:\\tmp", "c:foo", "bar"), "c:\\foo\\bar", "join c:\\tmp,c:foo,bar");
  do_check_eq(Win.winGetDrive("c:"), "c:");
  do_check_eq(Win.winGetDrive("c:\\"), "c:");
  do_check_eq(Win.winGetDrive("c:abc"), "c:");
  do_check_eq(Win.winGetDrive("c:abc\\d\\e\\f\\g"), "c:");
  do_check_eq(Win.winGetDrive("c:\\abc"), "c:");
  do_check_eq(Win.winGetDrive("c:\\abc\\d\\e\\f\\g"), "c:");

  do_print("Forwardslash-separated, no drive");
  do_check_eq(Win.normalize("/a/b/c"), "\\a\\b\\c");
  do_check_eq(Win.normalize("/a/b////c"), "\\a\\b\\c");
  do_check_eq(Win.normalize("/a/b/c///"), "\\a\\b\\c");
  do_check_eq(Win.normalize("/a/b/c/../../../d/e/f"), "\\d\\e\\f");
  do_check_eq(Win.normalize("a/b/c/../../../d/e/f"), "d\\e\\f");

  do_print("Forwardslash-separated, with a drive");
  do_check_eq(Win.normalize("c:/"), "c:\\");
  do_check_eq(Win.normalize("c:/a/b/c"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:/a/b////c"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:////a/b/c"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:/a/b/c///"), "c:\\a\\b\\c");
  do_check_eq(Win.normalize("c:/a/b/c/../../../d/e/f"), "c:\\d\\e\\f");
  do_check_eq(Win.normalize("c:a/b/c/../../../d/e/f"), "c:\\d\\e\\f");

  do_print("Backslash-separated, UNC-style");
  do_check_eq(Win.basename("\\\\a\\b"), "b");
  do_check_eq(Win.basename("\\\\a\\b\\"), "");
  do_check_eq(Win.basename("\\\\abc"), "");
  do_check_eq(Win.dirname("\\\\a\\b"), "\\\\a");
  do_check_eq(Win.dirname("\\\\a\\b\\"), "\\\\a\\b");
  do_check_eq(Win.dirname("\\\\a\\\\\\\\b"), "\\\\a");
  do_check_eq(Win.dirname("\\\\abc"), "\\\\abc");
  do_check_eq(Win.normalize("\\\\a\\b\\c"), "\\\\a\\b\\c");
  do_check_eq(Win.normalize("\\\\a\\b\\\\\\\\c"), "\\\\a\\b\\c");
  do_check_eq(Win.normalize("\\\\a\\b\\c\\\\\\"), "\\\\a\\b\\c");
  do_check_eq(Win.normalize("\\\\a\\b\\c\\..\\..\\d\\e\\f"), "\\\\a\\d\\e\\f");
  do_check_fail(() => Win.normalize("\\\\a\\b\\c\\..\\..\\..\\d\\e\\f"));

  do_check_eq(Win.join("\\\\a\\tmp", "foo", "bar"), "\\\\a\\tmp\\foo\\bar");
  do_check_eq(Win.join("\\\\a\\tmp", "\\foo", "bar"), "\\\\a\\foo\\bar");
  do_check_eq(Win.join("\\\\a\\tmp", "\\\\foo\\", "bar"), "\\\\foo\\bar");
  do_check_eq(Win.winGetDrive("\\\\"), null);
  do_check_eq(Win.winGetDrive("\\\\c"), "\\\\c");
  do_check_eq(Win.winGetDrive("\\\\c\\abc"), "\\\\c");

  do_print("Testing unix paths");
  do_check_eq(Unix.basename("a/b"), "b");
  do_check_eq(Unix.basename("a/b/"), "");
  do_check_eq(Unix.basename("abc"), "abc");
  do_check_eq(Unix.dirname("a/b"), "a");
  do_check_eq(Unix.dirname("a/b/"), "a/b");
  do_check_eq(Unix.dirname("a////b"), "a");
  do_check_eq(Unix.dirname("abc"), ".");
  do_check_eq(Unix.normalize("/a/b/c"), "/a/b/c");
  do_check_eq(Unix.normalize("/a/b////c"), "/a/b/c");
  do_check_eq(Unix.normalize("////a/b/c"), "/a/b/c");
  do_check_eq(Unix.normalize("/a/b/c///"), "/a/b/c");
  do_check_eq(Unix.normalize("/a/b/c/../../../d/e/f"), "/d/e/f");
  do_check_eq(Unix.normalize("a/b/c/../../../d/e/f"), "d/e/f");
  do_check_fail(() => Unix.normalize("/a/b/c/../../../../d/e/f"));

  do_check_eq(Unix.join("/tmp", "foo", "bar"), "/tmp/foo/bar", "join /tmp,foo,bar");
  do_check_eq(Unix.join("/tmp", "/foo", "bar"), "/foo/bar", "join /tmp,/foo,bar");

  do_print("Testing the presence of ospath.jsm");
  let Scope = {};
  try {
    Components.utils.import("resource://gre/modules/osfile/ospath.jsm", Scope);
  } catch (ex) {
    // Can't load ospath
  }
  do_check_true(!!Scope.basename);
}