๐Ÿ“ฆ leog / git-setup-submodules

๐Ÿ“„ index.test.js ยท 685 lines
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import fs from "fs";
import path from "path";

// Import functions to test
import setupSubmodules, { parseConfigLine, parseArgs, showHelp } from "../index.mjs";

describe("git-setup-submodules", () => {
  // ============================================
  // parseArgs tests
  // ============================================
  describe("parseArgs", () => {
    it("should return defaults when no arguments provided", () => {
      const options = parseArgs([]);
      expect(options).toEqual({
        config: ".git-setup-submodules",
        dryRun: false,
        defaultBranch: "main",
        help: false,
        quiet: false,
      });
    });

    it("should parse --help flag", () => {
      expect(parseArgs(["--help"]).help).toBe(true);
      expect(parseArgs(["-h"]).help).toBe(true);
    });

    it("should parse --dry-run flag", () => {
      expect(parseArgs(["--dry-run"]).dryRun).toBe(true);
      expect(parseArgs(["-n"]).dryRun).toBe(true);
    });

    it("should parse --quiet flag", () => {
      expect(parseArgs(["--quiet"]).quiet).toBe(true);
      expect(parseArgs(["-q"]).quiet).toBe(true);
    });

    it("should parse --config option", () => {
      expect(parseArgs(["--config", "custom.conf"]).config).toBe("custom.conf");
      expect(parseArgs(["-c", "another.conf"]).config).toBe("another.conf");
    });

    it("should parse --default-branch option", () => {
      expect(parseArgs(["--default-branch", "master"]).defaultBranch).toBe("master");
      expect(parseArgs(["-b", "develop"]).defaultBranch).toBe("develop");
    });

    it("should parse multiple flags together", () => {
      const options = parseArgs(["-n", "-q", "-c", "my.conf", "-b", "master"]);
      expect(options).toEqual({
        config: "my.conf",
        dryRun: true,
        defaultBranch: "master",
        help: false,
        quiet: true,
      });
    });

    it("should exit on unknown option", () => {
      const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {});
      const mockError = vi.spyOn(console, "error").mockImplementation(() => {});

      parseArgs(["--unknown"]);

      expect(mockExit).toHaveBeenCalledWith(1);
      expect(mockError).toHaveBeenCalledWith("Unknown option: --unknown");

      mockExit.mockRestore();
      mockError.mockRestore();
    });
  });

  // ============================================
  // parseConfigLine tests
  // ============================================
  describe("parseConfigLine", () => {
    describe("should skip non-config lines", () => {
      it("should return null for empty lines", () => {
        expect(parseConfigLine("")).toBeNull();
        expect(parseConfigLine("   ")).toBeNull();
        expect(parseConfigLine("\t")).toBeNull();
      });

      it("should return null for hash comments", () => {
        expect(parseConfigLine("# this is a comment")).toBeNull();
        expect(parseConfigLine("  # indented comment")).toBeNull();
      });

      it("should return null for slash comments", () => {
        expect(parseConfigLine("// this is a comment")).toBeNull();
        expect(parseConfigLine("  // indented comment")).toBeNull();
      });
    });

    describe("should parse basic module paths", () => {
      it("should parse simple module path", () => {
        const result = parseConfigLine("libs/utils");
        expect(result).toEqual({
          moduleName: "utils",
          modulePath: "libs/utils",
          localFolderName: "utils",
          destinationPath: "libs",
          submodulePath: "libs/utils",
          branchOrTag: "main",
          originalLine: "libs/utils",
        });
      });

      it("should parse single-level module path", () => {
        const result = parseConfigLine("mymodule");
        expect(result).toEqual({
          moduleName: "mymodule",
          modulePath: "mymodule",
          localFolderName: "mymodule",
          destinationPath: "",
          submodulePath: "mymodule",
          branchOrTag: "main",
          originalLine: "mymodule",
        });
      });

      it("should parse deeply nested module path", () => {
        const result = parseConfigLine("a/b/c/d/module");
        expect(result.moduleName).toBe("module");
        expect(result.destinationPath).toBe("a/b/c/d");
        expect(result.submodulePath).toBe("a/b/c/d/module");
      });
    });

    describe("should parse custom local paths", () => {
      it("should parse module with custom local path", () => {
        const result = parseConfigLine("apps/website:site");
        expect(result).toEqual({
          moduleName: "website",
          modulePath: "apps/website",
          localFolderName: "site",
          destinationPath: "apps",
          submodulePath: "apps/site",
          branchOrTag: "main",
          originalLine: "apps/website:site",
        });
      });

      it("should handle empty local path after colon", () => {
        const result = parseConfigLine("apps/website:");
        expect(result.localFolderName).toBe("website"); // Falls back to module name
      });
    });

    describe("should parse branch/tag specifications", () => {
      it("should parse module with branch", () => {
        const result = parseConfigLine("libs/logger#develop");
        expect(result.moduleName).toBe("logger");
        expect(result.branchOrTag).toBe("develop");
      });

      it("should parse module with tag", () => {
        const result = parseConfigLine("libs/logger#v1.2.3");
        expect(result.branchOrTag).toBe("v1.2.3");
      });

      it("should use custom default branch", () => {
        const result = parseConfigLine("libs/utils", "master");
        expect(result.branchOrTag).toBe("master");
      });

      it("should handle empty branch after hash", () => {
        const result = parseConfigLine("libs/utils#", "main");
        expect(result.branchOrTag).toBe("main"); // Falls back to default
      });
    });

    describe("should parse combined syntax", () => {
      it("should parse custom path and branch together", () => {
        const result = parseConfigLine("apps/website:site#production");
        expect(result).toEqual({
          moduleName: "website",
          modulePath: "apps/website",
          localFolderName: "site",
          destinationPath: "apps",
          submodulePath: "apps/site",
          branchOrTag: "production",
          originalLine: "apps/website:site#production",
        });
      });
    });

    describe("should handle inline comments", () => {
      it("should strip inline comments with space", () => {
        const result = parseConfigLine("libs/utils // this is utils");
        expect(result.moduleName).toBe("utils");
        expect(result.modulePath).toBe("libs/utils");
      });

      it("should strip inline comments without space", () => {
        const result = parseConfigLine("libs/utils//comment");
        expect(result.moduleName).toBe("utils");
      });

      it("should not strip // in URLs", () => {
        // This tests that we don't break paths that might contain ://
        // though in practice git paths don't have this
        const result = parseConfigLine("libs/utils");
        expect(result.modulePath).toBe("libs/utils");
      });
    });

    describe("should validate input", () => {
      it("should return error for empty module path", () => {
        const result = parseConfigLine(":#branch");
        expect(result.error).toBeDefined();
        expect(result.error).toContain("Empty module path");
      });

      it("should return error for paths with spaces", () => {
        const result = parseConfigLine("libs/my module");
        expect(result.error).toBeDefined();
        expect(result.error).toContain("Spaces not allowed");
      });

      it("should return error for paths with shell metacharacters", () => {
        const dangerous = ["libs/mod;rm", "libs/mod|cat", "libs/mod`id`", "libs/mod$(cmd)"];
        for (const path of dangerous) {
          const result = parseConfigLine(path);
          expect(result.error).toBeDefined();
          expect(result.error).toContain("Invalid characters");
        }
      });

      it("should return error for local path with invalid characters", () => {
        const result = parseConfigLine("libs/mod:bad;path");
        expect(result.error).toBeDefined();
        expect(result.error).toContain("Invalid characters");
      });
    });
  });

  // ============================================
  // setupSubmodules tests
  // ============================================
  describe("setupSubmodules", () => {
    const CONFIG_FILE = ".git-setup-submodules";
    let originalCwd;
    let mockExecSync;
    let gitmodulesExists;

    beforeEach(() => {
      vi.clearAllMocks();

      // Mock process.exit
      vi.spyOn(process, "exit").mockImplementation(() => {
        throw new Error("process.exit called");
      });

      // Mock console methods
      vi.spyOn(console, "log").mockImplementation(() => {});
      vi.spyOn(console, "error").mockImplementation(() => {});

      // Save original cwd
      originalCwd = process.cwd();

      // Create temp directory
      const testDir = path.join(process.cwd(), "temp-test");
      fs.mkdirSync(testDir, { recursive: true });
      process.chdir(testDir);

      // Reset state
      gitmodulesExists = false;

      // Mock execSync
      mockExecSync = vi.fn().mockImplementation((command) => {
        if (command.includes("git config --get remote.origin.url")) {
          return "git@github.com:user/repo.git";
        }
        if (command.startsWith("git ls-remote")) {
          return "";
        }
        return "";
      });

      // Mock fs.existsSync
      vi.spyOn(fs, "existsSync").mockImplementation((file) => {
        if (file === ".gitmodules") return gitmodulesExists;
        if (file === CONFIG_FILE) return true;
        return false;
      });

      // Mock fs.readFileSync
      vi.spyOn(fs, "readFileSync").mockImplementation((file) => {
        if (file === CONFIG_FILE) return "libs/utils";
        throw new Error(`Unexpected read: ${file}`);
      });

      // Mock fs.writeFileSync
      vi.spyOn(fs, "writeFileSync").mockImplementation((file) => {
        if (file === ".gitmodules") {
          gitmodulesExists = true;
        }
      });

      // Mock fs.accessSync
      vi.spyOn(fs, "accessSync").mockImplementation(() => {});
    });

    afterEach(() => {
      process.chdir(originalCwd);
      vi.restoreAllMocks();

      // Clean up temp directory
      const testDir = path.join(originalCwd, "temp-test");
      if (fs.existsSync(testDir)) {
        fs.rmSync(testDir, { recursive: true, force: true });
      }
    });

    describe("pre-flight checks", () => {
      it("should exit early if .gitmodules already exists", () => {
        gitmodulesExists = true;
        fs.existsSync.mockImplementation((file) => {
          if (file === ".gitmodules") return true;
          return false;
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.skipped).toBe(true);
        expect(result.success).toBe(0);
        expect(mockExecSync).not.toHaveBeenCalled();
      });

      it("should exit if config file is missing", () => {
        fs.existsSync.mockImplementation((file) => {
          if (file === ".gitmodules") return false;
          if (file === CONFIG_FILE) return false;
          return false;
        });

        expect(() => setupSubmodules(mockExecSync)).toThrow("process.exit");
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("Configuration file")
        );
      });

      it("should exit if not a git repository", () => {
        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            throw new Error("Not a git repository");
          }
          return "";
        });

        expect(() => setupSubmodules(mockExecSync)).toThrow("process.exit");
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("remote origin URL")
        );
      });
    });

    describe("happy path", () => {
      it("should process a single module correctly", () => {
        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.success).toBe(1);
        expect(result.failed).toBe(0);

        // Verify git commands were called
        expect(mockExecSync).toHaveBeenCalledWith(
          "git config --get remote.origin.url",
          { encoding: "utf8" }
        );
        expect(mockExecSync).toHaveBeenCalledWith(
          'git ls-remote "git@github.com:user/utils.git"',
          { stdio: "ignore" }
        );
        expect(mockExecSync).toHaveBeenCalledWith(
          'git submodule add --force "git@github.com:user/utils.git" "libs/utils"',
          { stdio: "ignore" }
        );
      });

      it("should process multiple modules", () => {
        fs.readFileSync.mockImplementation((file) => {
          if (file === CONFIG_FILE) {
            return `libs/utils
apps/console
libs/helpers:helpersLib#develop`;
          }
          throw new Error(`Unexpected read: ${file}`);
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.success).toBe(3);
        expect(result.failed).toBe(0);
      });

      it("should skip comments and empty lines", () => {
        fs.readFileSync.mockImplementation((file) => {
          if (file === CONFIG_FILE) {
            return `# Comment line
libs/utils

// Another comment
apps/console`;
          }
          throw new Error(`Unexpected read: ${file}`);
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.success).toBe(2);
      });

      it("should use custom default branch", () => {
        const result = setupSubmodules(mockExecSync, {
          quiet: true,
          defaultBranch: "master",
        });

        expect(mockExecSync).toHaveBeenCalledWith(
          expect.stringContaining('"master"'),
          expect.any(Object)
        );
      });

      it("should use custom config file", () => {
        fs.existsSync.mockImplementation((file) => {
          if (file === ".gitmodules") return gitmodulesExists;
          if (file === "custom.conf") return true;
          return false;
        });
        fs.readFileSync.mockImplementation((file) => {
          if (file === "custom.conf") return "libs/utils";
          throw new Error(`Unexpected read: ${file}`);
        });

        const result = setupSubmodules(mockExecSync, {
          quiet: true,
          config: "custom.conf",
        });

        expect(result.success).toBe(1);
      });
    });

    describe("dry run mode", () => {
      it("should not execute git commands in dry run", () => {
        const result = setupSubmodules(mockExecSync, { dryRun: true });

        expect(result.success).toBe(1);

        // Should call git config to get remote URL
        expect(mockExecSync).toHaveBeenCalledWith(
          "git config --get remote.origin.url",
          { encoding: "utf8" }
        );

        // Should NOT call submodule add
        expect(mockExecSync).not.toHaveBeenCalledWith(
          expect.stringContaining("git submodule add"),
          expect.any(Object)
        );
      });
    });

    describe("error handling", () => {
      it("should handle access denied errors", () => {
        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            return "git@github.com:user/repo.git";
          }
          if (command.startsWith("git ls-remote")) {
            const error = new Error("Permission denied");
            throw error;
          }
          return "";
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1);
        expect(result.success).toBe(0);
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("Access denied")
        );
      });

      it("should handle repository not found errors", () => {
        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            return "git@github.com:user/repo.git";
          }
          if (command.startsWith("git ls-remote")) {
            throw new Error("Repository not found");
          }
          return "";
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1);
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("not found")
        );
      });

      it("should handle network errors", () => {
        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            return "git@github.com:user/repo.git";
          }
          if (command.startsWith("git ls-remote")) {
            throw new Error("Could not resolve host");
          }
          return "";
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1);
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("Network error")
        );
      });

      it("should handle .gitmodules write permission errors", () => {
        fs.accessSync.mockImplementation((file, mode) => {
          if (file === ".gitmodules") {
            throw new Error("EACCES");
          }
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1);
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("Cannot write to .gitmodules")
        );
      });

      it("should handle git submodule add failures", () => {
        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            return "git@github.com:user/repo.git";
          }
          if (command.startsWith("git ls-remote")) {
            return "";
          }
          if (command.includes("git submodule add")) {
            throw new Error("Submodule add failed");
          }
          return "";
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1);
        expect(console.error).toHaveBeenCalledWith(
          expect.stringContaining("Failed to add")
        );
      });

      it("should continue processing after one module fails", () => {
        let callCount = 0;
        fs.readFileSync.mockImplementation((file) => {
          if (file === CONFIG_FILE) {
            return `libs/failing
libs/working`;
          }
          throw new Error(`Unexpected read: ${file}`);
        });

        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            return "git@github.com:user/repo.git";
          }
          if (command.startsWith("git ls-remote")) {
            callCount++;
            if (callCount === 1) {
              throw new Error("Access denied");
            }
            return "";
          }
          return "";
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1);
        expect(result.success).toBe(1);
      });

      it("should handle invalid config lines gracefully", () => {
        fs.readFileSync.mockImplementation((file) => {
          if (file === CONFIG_FILE) {
            return `libs/valid
libs/bad;path
libs/another-valid`;
          }
          throw new Error(`Unexpected read: ${file}`);
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.failed).toBe(1); // bad;path
        expect(result.success).toBe(2); // valid ones
      });
    });

    describe("HTTPS URL support", () => {
      it("should work with HTTPS remote URLs", () => {
        mockExecSync.mockImplementation((command) => {
          if (command.includes("git config --get remote.origin.url")) {
            return "https://github.com/user/repo.git";
          }
          if (command.startsWith("git ls-remote")) {
            return "";
          }
          return "";
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.success).toBe(1);
        expect(mockExecSync).toHaveBeenCalledWith(
          'git ls-remote "https://github.com/user/utils.git"',
          { stdio: "ignore" }
        );
      });
    });

    describe("empty config handling", () => {
      it("should handle empty config file", () => {
        fs.readFileSync.mockImplementation((file) => {
          if (file === CONFIG_FILE) return "";
          throw new Error(`Unexpected read: ${file}`);
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.success).toBe(0);
        expect(result.failed).toBe(0);
      });

      it("should handle config with only comments", () => {
        fs.readFileSync.mockImplementation((file) => {
          if (file === CONFIG_FILE) {
            return `# Just comments
// Nothing else

# More comments`;
          }
          throw new Error(`Unexpected read: ${file}`);
        });

        const result = setupSubmodules(mockExecSync, { quiet: true });

        expect(result.success).toBe(0);
        expect(result.failed).toBe(0);
      });
    });
  });

  // ============================================
  // showHelp tests
  // ============================================
  describe("showHelp", () => {
    it("should output help text", () => {
      const mockLog = vi.spyOn(console, "log").mockImplementation(() => {});

      showHelp();

      expect(mockLog).toHaveBeenCalled();
      const output = mockLog.mock.calls[0][0];
      expect(output).toContain("git-setup-submodules");
      expect(output).toContain("--help");
      expect(output).toContain("--dry-run");
      expect(output).toContain("--config");

      mockLog.mockRestore();
    });
  });
});