|
8 | 8 | determinePaths, |
9 | 9 | extractModuleInformation, |
10 | 10 | GoBinary, |
| 11 | + GoFileNameError, |
11 | 12 | parseGoVersion, |
12 | 13 | } from "../../lib/go-parser/go-binary"; |
13 | 14 | import { GoModule } from "../../lib/go-parser/go-module"; |
@@ -659,3 +660,77 @@ describe("test path determination", () => { |
659 | 660 | expect(vendorPath).toBe(""); |
660 | 661 | }); |
661 | 662 | }); |
| 663 | + |
| 664 | +describe("GoFileNameError", () => { |
| 665 | + // Construct a GoBinary from any real fixture so we have a valid instance to |
| 666 | + // call matchFilesToModules on. We then overwrite modules directly. |
| 667 | + const goBin = new GoBinary( |
| 668 | + elf.parse( |
| 669 | + readFileSync( |
| 670 | + path.join(__dirname, "../fixtures/go-binaries/go1.13.15_normal"), |
| 671 | + ), |
| 672 | + ), |
| 673 | + ); |
| 674 | + |
| 675 | + const moduleName = "github.com/foo/bar"; |
| 676 | + const moduleVersion = "v1.0.0"; |
| 677 | + |
| 678 | + // Crafting a file path where the module's fullName appears twice causes |
| 679 | + // pkgFile.split(modFullName) to produce 3 parts, triggering the throw. |
| 680 | + const duplicatedPath = `${moduleName}@${moduleVersion}/${moduleName}@${moduleVersion}/file.go`; |
| 681 | + |
| 682 | + beforeEach(() => { |
| 683 | + goBin.modules = [new GoModule(moduleName, moduleVersion)]; |
| 684 | + }); |
| 685 | + |
| 686 | + it("throws GoFileNameError when a file path cannot be matched to a module", () => { |
| 687 | + expect(() => goBin.matchFilesToModules([duplicatedPath])).toThrow( |
| 688 | + GoFileNameError, |
| 689 | + ); |
| 690 | + }); |
| 691 | + |
| 692 | + it("thrown error is an instance of Error", () => { |
| 693 | + expect(() => goBin.matchFilesToModules([duplicatedPath])).toThrow(Error); |
| 694 | + }); |
| 695 | + |
| 696 | + it("thrown error has a non-empty message containing the file and module names", () => { |
| 697 | + try { |
| 698 | + goBin.matchFilesToModules([duplicatedPath]); |
| 699 | + } catch (err) { |
| 700 | + expect(err).toBeInstanceOf(GoFileNameError); |
| 701 | + expect((err as GoFileNameError).message).toBeTruthy(); |
| 702 | + expect((err as GoFileNameError).message).toContain(duplicatedPath); |
| 703 | + expect((err as GoFileNameError).message).toContain( |
| 704 | + `${moduleName}@${moduleVersion}`, |
| 705 | + ); |
| 706 | + } |
| 707 | + }); |
| 708 | + |
| 709 | + it("thrown error has a stack trace", () => { |
| 710 | + try { |
| 711 | + goBin.matchFilesToModules([duplicatedPath]); |
| 712 | + } catch (err) { |
| 713 | + expect((err as GoFileNameError).stack).toBeDefined(); |
| 714 | + } |
| 715 | + }); |
| 716 | + |
| 717 | + it("thrown error carries fileName and moduleName properties", () => { |
| 718 | + try { |
| 719 | + goBin.matchFilesToModules([duplicatedPath]); |
| 720 | + } catch (err) { |
| 721 | + expect(err).toBeInstanceOf(GoFileNameError); |
| 722 | + expect((err as GoFileNameError).fileName).toBe(duplicatedPath); |
| 723 | + expect((err as GoFileNameError).moduleName).toBe( |
| 724 | + `${moduleName}@${moduleVersion}`, |
| 725 | + ); |
| 726 | + } |
| 727 | + }); |
| 728 | + |
| 729 | + it("thrown error has name set to GoFileNameError", () => { |
| 730 | + try { |
| 731 | + goBin.matchFilesToModules([duplicatedPath]); |
| 732 | + } catch (err) { |
| 733 | + expect((err as GoFileNameError).name).toBe("GoFileNameError"); |
| 734 | + } |
| 735 | + }); |
| 736 | +}); |
0 commit comments