addons/shared/__tests__/minimalDisambiguousPaths.test.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {minimalDisambiguousPaths} from '../minimalDisambiguousPaths';
b69ab319
b69ab3110describe('computeDisplayPaths', () => {
b69ab3111 it('should compute depth 1 paths correctly', () => {
b69ab3112 const input = ['/a/b.js', '/a/c.js'];
b69ab3113 const expected = ['b.js', 'c.js'];
b69ab3114 const actual = minimalDisambiguousPaths(input);
b69ab3115 expect(actual).toEqual(expected);
b69ab3116 });
b69ab3117
b69ab3118 it('should compute depth 2 paths correctly', () => {
b69ab3119 const input = ['/a/b.js', '/c/b.js'];
b69ab3120 const expected = ['/a/b.js', '/c/b.js'];
b69ab3121 const actual = minimalDisambiguousPaths(input);
b69ab3122 expect(actual).toEqual(expected);
b69ab3123 });
b69ab3124
b69ab3125 it('should compute a mix of depths 1 to 5 paths correctly', () => {
b69ab3126 const input = [
b69ab3127 '/a/b/c/d/e.js',
b69ab3128 '/z/b/c/d/e.js',
b69ab3129 'a/y/z/e.js',
b69ab3130 'a/y/e.js',
b69ab3131 'a/z/e.js',
b69ab3132 'a/f.js',
b69ab3133 ];
b69ab3134 const expected = ['/a/b/c/d/e.js', '/z/b/c/d/e.js', 'y/z/e.js', 'y/e.js', 'a/z/e.js', 'f.js'];
b69ab3135 const actual = minimalDisambiguousPaths(input);
b69ab3136 expect(actual).toEqual(expected);
b69ab3137 });
b69ab3138
b69ab3139 it('should respect alwaysShowLeadingSeparator = false', () => {
b69ab3140 const input = [
b69ab3141 '/a/b/c/d/e.js',
b69ab3142 '/z/b/c/d/e.js',
b69ab3143 'a/y/z/e.js',
b69ab3144 'a/y/e.js',
b69ab3145 'a/z/e.js',
b69ab3146 'a/f.js',
b69ab3147 ];
b69ab3148 const expected = ['/a/b/c/d/e.js', '/z/b/c/d/e.js', 'y/z/e.js', 'y/e.js', 'a/z/e.js', 'f.js'];
b69ab3149 const actual = minimalDisambiguousPaths(input, {alwaysShowLeadingSeparator: false});
b69ab3150 expect(actual).toEqual(expected);
b69ab3151 });
b69ab3152
b69ab3153 it('should handle duplicate paths correctly', () => {
b69ab3154 const input = ['/a/b/c/d/e.js', '/a/b/c/d/e.js'];
b69ab3155 const expected = ['/a/b/c/d/e.js', '/a/b/c/d/e.js'];
b69ab3156 const actual = minimalDisambiguousPaths(input);
b69ab3157 expect(actual).toEqual(expected);
b69ab3158 });
b69ab3159
b69ab3160 it('should handle paths that appear as a subpath of another path correctly', () => {
b69ab3161 const input = ['/a/b/c/d/e.js', '/c/d/e.js'];
b69ab3162 const expected = ['/b/c/d/e.js', '/c/d/e.js'];
b69ab3163 const actual = minimalDisambiguousPaths(input);
b69ab3164 expect(actual).toEqual(expected);
b69ab3165 });
b69ab3166
b69ab3167 it('should honor the max depth argument', () => {
b69ab3168 const input = ['/a/b/c/d/e.js', '/z/b/c/d/e.js'];
b69ab3169 const expected = ['/d/e.js', '/d/e.js'];
b69ab3170 const actual = minimalDisambiguousPaths(input, {maxDepth: 2});
b69ab3171 expect(actual).toEqual(expected);
b69ab3172 });
b69ab3173
b69ab3174 it('should handle empty input', () => {
b69ab3175 const input: string[] = [];
b69ab3176 const expected: string[] = [];
b69ab3177 const actual = minimalDisambiguousPaths(input);
b69ab3178 expect(actual).toEqual(expected);
b69ab3179 });
b69ab3180
b69ab3181 it('should handle a realistic scenario path separators', () => {
b69ab3182 const input = [
b69ab3183 '/data/users/x/fbsource/fbcode/foo/bar.js',
b69ab3184 '/data/users/x/fbsource/fbcode/bar/bar.js',
b69ab3185 '/Users/y/fbsource/fbcode/foo/bar.js',
b69ab3186 '/Users/y/fbsource/fbcode/bar/bar.js',
b69ab3187 ];
b69ab3188 const expected = [
b69ab3189 '/x/fbsource/fbcode/foo/bar.js',
b69ab3190 '/x/fbsource/fbcode/bar/bar.js',
b69ab3191 '/y/fbsource/fbcode/foo/bar.js',
b69ab3192 '/y/fbsource/fbcode/bar/bar.js',
b69ab3193 ];
b69ab3194 const actual = minimalDisambiguousPaths(input);
b69ab3195 expect(actual).toEqual(expected);
b69ab3196 });
b69ab3197
b69ab3198 it('should handle root correctly (maintain the "/")', () => {
b69ab3199 const input = ['/', '/foo'];
b69ab31100 const expected = ['/', 'foo'];
b69ab31101 const actual = minimalDisambiguousPaths(input);
b69ab31102 expect(actual).toEqual(expected);
b69ab31103 });
b69ab31104
b69ab31105 it('should handle relative paths correctly (not insert a new "/")', () => {
b69ab31106 const input = ['hello/hi.txt', 'baz/bar.js', 'foo/bar.js'];
b69ab31107 const expected = ['hi.txt', 'baz/bar.js', 'foo/bar.js'];
b69ab31108 const actual = minimalDisambiguousPaths(input);
b69ab31109 expect(actual).toEqual(expected);
b69ab31110 });
b69ab31111
b69ab31112 it('should handle trailing slashes correctly', () => {
b69ab31113 const input = ['/foo/'];
b69ab31114 const expected = ['foo'];
b69ab31115 const actual = minimalDisambiguousPaths(input);
b69ab31116 expect(actual).toEqual(expected);
b69ab31117 });
b69ab31118
b69ab31119 it('should handle windows paths without ambiguity correctly', () => {
b69ab31120 const input = ['c:\\foo\\baz.js', 'c:\\foo\\bar.js'];
b69ab31121 const expected = ['baz.js', 'bar.js'];
b69ab31122 const actual = minimalDisambiguousPaths(input);
b69ab31123 expect(actual).toEqual(expected);
b69ab31124 });
b69ab31125
b69ab31126 it('should handle windows paths with ambiguity correctly', () => {
b69ab31127 const input = ['c:\\foo\\bar\\baz.js', 'c:\\foo\\bar2\\baz.js'];
b69ab31128 const expected = ['\\bar\\baz.js', '\\bar2\\baz.js'];
b69ab31129 const actual = minimalDisambiguousPaths(input);
b69ab31130 expect(actual).toEqual(expected);
b69ab31131 });
b69ab31132
b69ab31133 it('should include the prefix on windows when it is showing the full path', () => {
b69ab31134 const input = ['c:\\foo2\\bar.js', 'c:\\foo\\bar.js'];
b69ab31135 const expected = ['c:\\foo2\\bar.js', 'c:\\foo\\bar.js'];
b69ab31136 const actual = minimalDisambiguousPaths(input);
b69ab31137 expect(actual).toEqual(expected);
b69ab31138 });
b69ab31139
b69ab31140 it('should hide windows path drive prefix when paths are minimized', () => {
b69ab31141 const input = ['c:\\foo\\bar\\boo.js', 'c:\\foo\\bar2\\blam.js'];
b69ab31142 const expected = ['boo.js', 'blam.js'];
b69ab31143 const actual = minimalDisambiguousPaths(input);
b69ab31144 expect(actual).toEqual(expected);
b69ab31145 });
b69ab31146});