Merge branch 'v0.4'
[platform/upstream/nodejs.git] / test / simple / test-path.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 var common = require('../common');
23 var assert = require('assert');
24
25 var path = require('path');
26
27 var isWindows = process.platform === 'win32';
28
29 var f = __filename;
30
31 assert.equal(path.basename(f), 'test-path.js');
32 assert.equal(path.basename(f, '.js'), 'test-path');
33 assert.equal(path.extname(f), '.js');
34 assert.equal(path.dirname(f).substr(-11), isWindows ? 'test\\simple' : 'test/simple');
35 assert.equal(path.dirname('/a/b/'), '/a');
36 assert.equal(path.dirname('/a/b'), '/a');
37 assert.equal(path.dirname('/a'), '/');
38 assert.equal(path.dirname('/'), '/');
39 path.exists(f, function(y) { assert.equal(y, true) });
40
41 assert.equal(path.existsSync(f), true);
42
43 assert.equal(path.extname(''), '');
44 assert.equal(path.extname('/path/to/file'), '');
45 assert.equal(path.extname('/path/to/file.ext'), '.ext');
46 assert.equal(path.extname('/path.to/file.ext'), '.ext');
47 assert.equal(path.extname('/path.to/file'), '');
48 assert.equal(path.extname('/path.to/.file'), '');
49 assert.equal(path.extname('/path.to/.file.ext'), '.ext');
50 assert.equal(path.extname('/path/to/f.ext'), '.ext');
51 assert.equal(path.extname('/path/to/..ext'), '.ext');
52 assert.equal(path.extname('file'), '');
53 assert.equal(path.extname('file.ext'), '.ext');
54 assert.equal(path.extname('.file'), '');
55 assert.equal(path.extname('.file.ext'), '.ext');
56 assert.equal(path.extname('/file'), '');
57 assert.equal(path.extname('/file.ext'), '.ext');
58 assert.equal(path.extname('/.file'), '');
59 assert.equal(path.extname('/.file.ext'), '.ext');
60 assert.equal(path.extname('.path/file.ext'), '.ext');
61 assert.equal(path.extname('file.ext.ext'), '.ext');
62 assert.equal(path.extname('file.'), '.');
63
64 // path.join tests
65 var failures = [];
66 var joinTests =
67     // arguments                     result
68     [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
69      [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
70      [['/foo', '../../../bar'], '/bar'],
71      [['foo', '../../../bar'], '../../bar'],
72      [['foo/', '../../../bar'], '../../bar'],
73      [['foo/x', '../../../bar'], '../bar'],
74      [['foo/x', './bar'], 'foo/x/bar'],
75      [['foo/x/', './bar'], 'foo/x/bar'],
76      [['foo/x/', '.', 'bar'], 'foo/x/bar'],
77      [['./'], './'],
78      [['.', './'], './'],
79      [['.', '.', '.'], '.'],
80      [['.', './', '.'], '.'],
81      [['.', '/./', '.'], '.'],
82      [['.', '/////./', '.'], '.'],
83      [['.'], '.'],
84      [['', '.'], '.'],
85      [['', 'foo'], 'foo'],
86      [['foo', '/bar'], 'foo/bar'],
87      [['', '/foo'], '/foo'],
88      [['', '', '/foo'], '/foo'],
89      [['', '', 'foo'], 'foo'],
90      [['foo', ''], 'foo'],
91      [['foo/', ''], 'foo/'],
92      [['foo', '', '/bar'], 'foo/bar'],
93      [['./', '..', '/foo'], '../foo'],
94      [['./', '..', '..', '/foo'], '../../foo'],
95      [['.', '..', '..', '/foo'], '../../foo'],
96      [['', '..', '..', '/foo'], '../../foo'],
97      [['/'], '/'],
98      [['/', '.'], '/'],
99      [['/', '..'], '/'],
100      [['/', '..', '..'], '/'],
101      [[''], '.'],
102      [['', ''], '.'],
103      [[' /foo'], ' /foo'],
104      [[' ', 'foo'], ' /foo'],
105      [[' ', '.'], ' '],
106      [[' ', '/'], ' /'],
107      [[' ', ''], ' '],
108      // filtration of non-strings.
109      [['x', true, 7, 'y', null, {}], 'x/y']
110     ];
111 joinTests.forEach(function(test) {
112   var actual = path.join.apply(path, test[0]);
113   var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1];
114   var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
115                 '\n  expect=' + JSON.stringify(expected) +
116                 '\n  actual=' + JSON.stringify(actual);
117   if (actual !== expected) failures.push('\n' + message);
118   // assert.equal(actual, expected, message);
119 });
120 assert.equal(failures.length, 0, failures.join(''));
121
122 // path normalize tests
123 if (isWindows) {
124   assert.equal(path.normalize('./fixtures///b/../b/c.js'),
125                'fixtures\\b\\c.js');
126   assert.equal(path.normalize('/foo/../../../bar'), '\\bar');
127   assert.equal(path.normalize('a//b//../b'), 'a\\b');
128   assert.equal(path.normalize('a//b//./c'), 'a\\b\\c');
129   assert.equal(path.normalize('a//b//.'), 'a\\b');
130 } else {
131   assert.equal(path.normalize('./fixtures///b/../b/c.js'),
132                'fixtures/b/c.js');
133   assert.equal(path.normalize('/foo/../../../bar'), '/bar');
134   assert.equal(path.normalize('a//b//../b'), 'a/b');
135   assert.equal(path.normalize('a//b//./c'), 'a/b/c');
136   assert.equal(path.normalize('a//b//.'), 'a/b');
137 }
138
139 // path.resolve tests
140 if (isWindows) {
141   // windows
142   var resolveTests =
143     // arguments                                    result
144     [[['c:/blah\\blah', 'd:/games', 'c:../a'     ], 'c:\\blah\\a'    ],
145      [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'    ], 'd:\\e.exe'      ],
146      [['c:/ignore', 'c:/some/file'               ], 'c:\\some\\file' ],
147      [['d:/ignore', 'd:some/dir//'               ], 'd:\\ignore\\some\\dir' ],
148      [['.'                                       ], process.cwd()    ],
149      [['//server/share', '..', 'relative\\'      ], '\\\\server\\share\\relative' ]];
150 } else {
151   // Posix
152   var resolveTests =
153     // arguments                                    result
154     [[['/var/lib', '../', 'file/'                ], '/var/file'      ],
155      [['/var/lib', '/../', 'file/'               ], '/file'          ],
156      [['a/b/c/', '../../..'                      ], process.cwd()    ],
157      [['.'                                       ], process.cwd()    ],
158      [['/some/dir', '.', '/absolute/'            ], '/absolute'      ]];
159 }
160 var failures = []
161 resolveTests.forEach(function(test) {
162   var actual = path.resolve.apply(path, test[0]);
163   var expected = test[1];
164   var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
165                 '\n  expect=' + JSON.stringify(expected) +
166                 '\n  actual=' + JSON.stringify(actual);
167   if (actual !== expected) failures.push('\n' + message);
168   // assert.equal(actual, expected, message);
169 });
170 assert.equal(failures.length, 0, failures.join(''));
171
172 // path.relative tests
173 if (isWindows) {
174   // windows
175   var relativeTests =
176    // arguments                     result
177    [['c:/blah\\blah', 'd:/games',   'd:\\games'],
178     ['c:/aaaa/bbbb', 'c:/aaaa',     '..'],
179     ['c:/aaaa/bbbb', 'c:/cccc',     '..\\..\\cccc'],
180     ['c:/aaaa/bbbb', 'c:/aaaa/bbbb',''],
181     ['c:/aaaa/bbbb', 'c:/aaaa/cccc','..\\cccc'],
182     ['c:/aaaa/', 'c:/aaaa/cccc',    'cccc'],
183     ['c:/', 'c:\\aaaa\\bbbb',       'aaaa\\bbbb'],
184     ['c:/aaaa/bbbb', 'd:\\',        'd:\\']];
185 } else {
186   // posix
187   var relativeTests =
188     // arguments                    result
189     [['/var/lib', '/var',           '..'],
190      ['/var/lib', '/bin',           '../../bin'],
191      ['/var/lib', '/var/lib',       ''],
192      ['/var/lib', '/var/apache',    '../apache'],
193      ['/var/', '/var/lib',          'lib'],
194      ['/', '/var/lib',              'var/lib']];
195 }
196 var failures = [];
197 relativeTests.forEach(function(test) {
198   var actual = path.relative(test[0], test[1]);
199   var expected = test[2];
200   var message = 'path.relative(' + test.slice(0, 2).map(JSON.stringify).join(',') + ')' +
201                 '\n  expect=' + JSON.stringify(expected) +
202                 '\n  actual=' + JSON.stringify(actual);
203   if (actual !== expected) failures.push('\n' + message);
204 });
205 assert.equal(failures.length, 0, failures.join(''));
206