|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import '../../test/mockRushCommandLineParser'; |
| 5 | + |
| 6 | +import * as path from 'path'; |
| 7 | + |
| 8 | +import { TabCompleteAction } from '../TabCompleteAction'; |
| 9 | +import { RushCommandLineParser } from '../../RushCommandLineParser'; |
| 10 | + |
| 11 | +function arrayEqual(actual: string[], expected: string[]): boolean { |
| 12 | + return ( |
| 13 | + actual.length === expected.length && |
| 14 | + actual.sort().every((v: string, i: number) => { |
| 15 | + return v === expected.sort()[i]; |
| 16 | + }) |
| 17 | + ); |
| 18 | +} |
| 19 | + |
| 20 | +describe('TabCompleteAction', () => { |
| 21 | + let oldExitCode: number | undefined; |
| 22 | + let oldArgs: string[]; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + jest.spyOn(process, 'exit').mockImplementation(); |
| 26 | + oldExitCode = process.exitCode; |
| 27 | + oldArgs = process.argv; |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + jest.clearAllMocks(); |
| 32 | + process.exitCode = oldExitCode; |
| 33 | + process.argv = oldArgs; |
| 34 | + }); |
| 35 | + |
| 36 | + describe(`Gets TabCompletions`, () => { |
| 37 | + it(`gets completion(s) for rush <tab>`, () => { |
| 38 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 39 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 40 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 41 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 42 | + // ends. |
| 43 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 44 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 45 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 46 | + |
| 47 | + const commandLine: string = 'rush'; |
| 48 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 49 | + |
| 50 | + expect(actual.indexOf('add') !== -1).toBe(true); |
| 51 | + expect(actual.indexOf('check') !== -1).toBe(true); |
| 52 | + expect(actual.indexOf('build') !== -1).toBe(true); |
| 53 | + expect(actual.indexOf('rebuild') !== -1).toBe(true); |
| 54 | + expect(actual.indexOf('-d') !== -1).toBe(true); |
| 55 | + expect(actual.indexOf('--debug') !== -1).toBe(true); |
| 56 | + expect(actual.indexOf('--help') !== -1).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it(`gets completion(s) for rush a<tab>`, () => { |
| 60 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 61 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 62 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 63 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 64 | + // ends. |
| 65 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 66 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 67 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 68 | + |
| 69 | + const commandLine: string = 'rush a'; |
| 70 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 71 | + |
| 72 | + const expected: string[] = ['add']; |
| 73 | + |
| 74 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + it(`gets completion(s) for rush build <tab>`, () => { |
| 78 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 79 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 80 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 81 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 82 | + // ends. |
| 83 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 84 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 85 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 86 | + |
| 87 | + const commandLine: string = 'rush build '; |
| 88 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 89 | + |
| 90 | + expect(actual.indexOf('-t') !== -1).toBe(true); |
| 91 | + expect(actual.indexOf('--to') !== -1).toBe(true); |
| 92 | + expect(actual.indexOf('-f') !== -1).toBe(true); |
| 93 | + expect(actual.indexOf('--from') !== -1).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it(`gets completion(s) for rush build -<tab>`, () => { |
| 97 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 98 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 99 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 100 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 101 | + // ends. |
| 102 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 103 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 104 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 105 | + |
| 106 | + const commandLine: string = 'rush build -'; |
| 107 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 108 | + |
| 109 | + expect(actual.indexOf('-t') !== -1).toBe(true); |
| 110 | + expect(actual.indexOf('--to') !== -1).toBe(true); |
| 111 | + expect(actual.indexOf('-f') !== -1).toBe(true); |
| 112 | + expect(actual.indexOf('--from') !== -1).toBe(true); |
| 113 | + }); |
| 114 | + |
| 115 | + it(`gets completion(s) for rush build -t <tab>`, () => { |
| 116 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 117 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 118 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 119 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 120 | + // ends. |
| 121 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 122 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 123 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 124 | + |
| 125 | + const commandLine: string = 'rush build -t '; |
| 126 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 127 | + |
| 128 | + const expected: string[] = ['abc', 'def']; |
| 129 | + |
| 130 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 131 | + }); |
| 132 | + |
| 133 | + it(`gets completion(s) for rush build -t a<tab>`, () => { |
| 134 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 135 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 136 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 137 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 138 | + // ends. |
| 139 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 140 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 141 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 142 | + |
| 143 | + const commandLine: string = 'rush build -t a'; |
| 144 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 145 | + |
| 146 | + const expected: string[] = ['abc']; |
| 147 | + |
| 148 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + it(`gets completion(s) for rush change --bump-type <tab>`, () => { |
| 152 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 153 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 154 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 155 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 156 | + // ends. |
| 157 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 158 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 159 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 160 | + |
| 161 | + const commandLine: string = 'rush change --bump-type '; |
| 162 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 163 | + |
| 164 | + const expected: string[] = ['major', 'minor', 'patch', 'none']; |
| 165 | + |
| 166 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 167 | + }); |
| 168 | + |
| 169 | + it(`gets completion(s) for rush change --bulk <tab>`, () => { |
| 170 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 171 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 172 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 173 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 174 | + // ends. |
| 175 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 176 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 177 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 178 | + |
| 179 | + const commandLine: string = 'rush change --bulk '; |
| 180 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 181 | + |
| 182 | + expect(actual.indexOf('--bulk') !== -1).toBe(true); |
| 183 | + expect(actual.indexOf('--message') !== -1).toBe(true); |
| 184 | + expect(actual.indexOf('--bump-type') !== -1).toBe(true); |
| 185 | + expect(actual.indexOf('--verify') !== -1).toBe(true); |
| 186 | + }); |
| 187 | + |
| 188 | + it(`gets completion(s) for rush change --bump-type m<tab>`, () => { |
| 189 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 190 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 191 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 192 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 193 | + // ends. |
| 194 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 195 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 196 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 197 | + |
| 198 | + const commandLine: string = 'rush change --bump-type m'; |
| 199 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 200 | + |
| 201 | + const expected: string[] = ['major', 'minor']; |
| 202 | + |
| 203 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 204 | + }); |
| 205 | + |
| 206 | + it(`gets completion(s) for rush change --message <tab>`, () => { |
| 207 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 208 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 209 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 210 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 211 | + // ends. |
| 212 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 213 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 214 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 215 | + |
| 216 | + const commandLine: string = 'rush change --message '; |
| 217 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 218 | + |
| 219 | + const expected: string[] = []; |
| 220 | + |
| 221 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 222 | + }); |
| 223 | + |
| 224 | + it(`gets completion(s) for rush change --message "my change log message" --bump-type <tab>`, () => { |
| 225 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 226 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 227 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 228 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 229 | + // ends. |
| 230 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 231 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 232 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 233 | + |
| 234 | + const commandLine: string = 'rush change --message "my change log message" --bump-type '; |
| 235 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 236 | + |
| 237 | + const expected: string[] = ['major', 'minor', 'patch', 'none']; |
| 238 | + |
| 239 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 240 | + }); |
| 241 | + |
| 242 | + it(`gets completion(s) for rush change --message "my change log message" --bump-type m<tab>`, () => { |
| 243 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 244 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 245 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 246 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 247 | + // ends. |
| 248 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 249 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 250 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 251 | + |
| 252 | + const commandLine: string = 'rush change --message "my change log message" --bump-type m'; |
| 253 | + const actual: string[] = Array.from(tc.getCompletions(commandLine.trim(), commandLine.length)); |
| 254 | + |
| 255 | + const expected: string[] = ['major', 'minor']; |
| 256 | + |
| 257 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
| 261 | + describe(`Tokenize command line`, () => { |
| 262 | + it(`tokenizes "rush change -"`, () => { |
| 263 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 264 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 265 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 266 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 267 | + // ends. |
| 268 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 269 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 270 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 271 | + |
| 272 | + const commandLine: string = 'rush change -'; |
| 273 | + const actual: string[] = Array.from(tc.tokenizeCommandLine(commandLine.trim())); |
| 274 | + |
| 275 | + const expected: string[] = ['rush', 'change', '-']; |
| 276 | + |
| 277 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 278 | + }); |
| 279 | + it(`tokenizes 'rush change -m "my change log"'`, () => { |
| 280 | + const startPath: string = path.resolve(__dirname, 'tabComplete'); |
| 281 | + // Create a Rush CLI instance. This instance is heavy-weight and relies on setting process.exit |
| 282 | + // to exit and clear the Rush file lock. So running multiple `it` or `describe` test blocks over the same test |
| 283 | + // repo will fail due to contention over the same lock which is kept until the test runner process |
| 284 | + // ends. |
| 285 | + jest.spyOn(process, 'cwd').mockReturnValue(startPath); |
| 286 | + const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: startPath }); |
| 287 | + const tc: TabCompleteAction = new TabCompleteAction(parser); |
| 288 | + |
| 289 | + const commandLine: string = 'rush change -m "my change log"'; |
| 290 | + const actual: string[] = Array.from(tc.tokenizeCommandLine(commandLine.trim())); |
| 291 | + |
| 292 | + const expected: string[] = ['rush', 'change', '-m', 'my change log']; |
| 293 | + |
| 294 | + expect(arrayEqual(actual, expected)).toBe(true); |
| 295 | + }); |
| 296 | + }); |
| 297 | +}); |
0 commit comments