@@ -470,7 +470,7 @@ func TestConflictingPackageRegex(t *testing.T) {
470470 name : "perl with epoch" ,
471471 inputLine : "D: ========== +++ perl-4:5.34.1-489.cm2 x86_64-linux 0x0" ,
472472 expectedMatch : true ,
473- expectedOutput : "perl-5.34.1-489.cm2.x86_64" ,
473+ expectedOutput : "perl-4: 5.34.1-489.cm2.x86_64" ,
474474 },
475475 {
476476 name : "systemd no epoch" ,
@@ -494,3 +494,234 @@ func TestConflictingPackageRegex(t *testing.T) {
494494 })
495495 }
496496}
497+
498+ func TestPackageFQNRegexWithValidInput (t * testing.T ) {
499+ tests := []struct {
500+ name string
501+ input string
502+ expectedGroups []string
503+ }{
504+ {
505+ name : "package with epoch and architecture" ,
506+ input : "pkg-name-0:1.2.3-4.azl3.x86_64.rpm" ,
507+ expectedGroups : []string {"pkg-name" , "0" , "1.2.3" , "4.azl3" , "x86_64" , "rpm" },
508+ },
509+ {
510+ name : "package with epoch and architecture but no '.rpm' suffix" ,
511+ input : "pkg-name-0:1.2.3-4.azl3.x86_64" ,
512+ expectedGroups : []string {"pkg-name" , "0" , "1.2.3" , "4.azl3" , "x86_64" , "" },
513+ },
514+ {
515+ name : "package without epoch, and architecture" ,
516+ input : "pkg-name-1.2.3-4.azl3.rpm" ,
517+ expectedGroups : []string {"pkg-name" , "" , "1.2.3" , "4.azl3" , "" , "rpm" },
518+ },
519+ {
520+ name : "package with architecture but no epoch" ,
521+ input : "pkg-name-1.2.3-4.azl3.aarch64" ,
522+ expectedGroups : []string {"pkg-name" , "" , "1.2.3" , "4.azl3" , "aarch64" , "" },
523+ },
524+ {
525+ name : "package with epoch but no architecture" ,
526+ input : "pkg-name-0:1.2.3-4.azl3" ,
527+ expectedGroups : []string {"pkg-name" , "0" , "1.2.3" , "4.azl3" , "" , "" },
528+ },
529+ {
530+ name : "package without '.rpm' suffix" ,
531+ input : "pkg-name-1.2.3-4.azl3.x86_64" ,
532+ expectedGroups : []string {"pkg-name" , "" , "1.2.3" , "4.azl3" , "x86_64" , "" },
533+ },
534+ {
535+ name : "package with version containing the '+' character" ,
536+ input : "pkg-name-1.2.3+4-4.azl3.x86_64.rpm" ,
537+ expectedGroups : []string {"pkg-name" , "" , "1.2.3+4" , "4.azl3" , "x86_64" , "rpm" },
538+ },
539+ {
540+ name : "package with version containing the '~' character" ,
541+ input : "pkg-name-1.2.3~4-4.azl3.x86_64.rpm" ,
542+ expectedGroups : []string {"pkg-name" , "" , "1.2.3~4" , "4.azl3" , "x86_64" , "rpm" },
543+ },
544+ {
545+ name : "package with release containing two '.' characters" ,
546+ input : "pkg-name-1.2.3-4.5.azl3.x86_64.rpm" ,
547+ expectedGroups : []string {"pkg-name" , "" , "1.2.3" , "4.5.azl3" , "x86_64" , "rpm" },
548+ },
549+ {
550+ name : "package with release containing the '_' character" ,
551+ input : "pkg-name-1.2.3-45.az_l3.x86_64.rpm" ,
552+ expectedGroups : []string {"pkg-name" , "" , "1.2.3" , "45.az_l3" , "x86_64" , "rpm" },
553+ },
554+ {
555+ name : "package with release containing the `~` character" ,
556+ input : "pkg-name-1.2.3-45.azl3~2.x86_64.rpm" ,
557+ expectedGroups : []string {"pkg-name" , "" , "1.2.3" , "45.azl3~2" , "x86_64" , "rpm" },
558+ },
559+ {
560+ name : "package with double dash in name" ,
561+ input : "nvidia-container-toolkit-1.15.0-1.azl3.x86_64.rpm" ,
562+ expectedGroups : []string {"nvidia-container-toolkit" , "" , "1.15.0" , "1.azl3" , "x86_64" , "rpm" },
563+ },
564+ {
565+ name : "package with underscore in release" ,
566+ input : "nvidia-container-toolkit-550.54.15-2_5.15.162.2.1.azl3.x86_64.rpm" ,
567+ expectedGroups : []string {"nvidia-container-toolkit" , "" , "550.54.15" , "2_5.15.162.2.1.azl3" , "x86_64" , "rpm" },
568+ },
569+ }
570+
571+ for _ , tt := range tests {
572+ t .Run (tt .name , func (t * testing.T ) {
573+ matches := packageFQNRegex .FindStringSubmatch (tt .input )
574+ assert .NotNil (t , matches )
575+ assert .Equal (t , tt .expectedGroups , matches [1 :])
576+ })
577+ }
578+ }
579+
580+ func TestPackageFQNRegexWithInvalidInput (t * testing.T ) {
581+ tests := []struct {
582+ name string
583+ input string
584+ }{
585+ {
586+ name : "package with missing version" ,
587+ input : "pkg-name--4.azl3.x86_64.rpm" ,
588+ },
589+ {
590+ name : "package with missing release" ,
591+ input : "pkg-name-1.2.3-.azl3.x86_64.rpm" ,
592+ },
593+ {
594+ name : "package with missing name" ,
595+ input : "-1.2.3-4.azl3.x86_64.rpm" ,
596+ },
597+ {
598+ name : "package with only hyphen" ,
599+ input : "-" ,
600+ },
601+ {
602+ name : "package with version not beginning with a digit" ,
603+ input : "pkg-name-0:a1.2.3-4.azl3.x86_64.rpm" ,
604+ },
605+ {
606+ name : "package with release not beginning with a digit" ,
607+ input : "pkg-name-0:1.2.3-D4.azl3.x86_64.rpm" ,
608+ },
609+ {
610+ name : "package with epoch not beginning with a digit" ,
611+ input : "pkg-name-0:1.2.3-D4.azl3.x86_64.rpm" ,
612+ },
613+ {
614+ name : "package with epoch unsupported architecture" ,
615+ input : "pkg-name-0:1.2.3-D4.azl3.other_arch.rpm" ,
616+ },
617+ }
618+
619+ for _ , tt := range tests {
620+ t .Run (tt .name , func (t * testing.T ) {
621+ matches := packageFQNRegex .FindStringSubmatch (tt .input )
622+ assert .Nil (t , matches )
623+ })
624+ }
625+ }
626+
627+ func TestStripEpochFromPackageFullQualifiedNameWithValidInput (t * testing.T ) {
628+ tests := []struct {
629+ name string
630+ input string
631+ expected string
632+ }{
633+ {
634+ name : "package with epoch and architecture" ,
635+ input : "pkg-name-0:1.2.3-4.azl3.x86_64.rpm" ,
636+ expected : "pkg-name-1.2.3-4.azl3.x86_64.rpm" ,
637+ },
638+ {
639+ name : "package with epoch and architecture but no '.rpm' suffix" ,
640+ input : "pkg-name-0:1.2.3-4.azl3.x86_64" ,
641+ expected : "pkg-name-1.2.3-4.azl3.x86_64" ,
642+ },
643+ {
644+ name : "package with epoch but no architecture" ,
645+ input : "pkg-name-0:1.2.3-4.azl3" ,
646+ expected : "pkg-name-1.2.3-4.azl3" ,
647+ },
648+ {
649+ name : "package with architecture but no epoch" ,
650+ input : "pkg-name-1.2.3-4.azl3.aarch64" ,
651+ expected : "pkg-name-1.2.3-4.azl3.aarch64" ,
652+ },
653+ {
654+ name : "package without epoch, and architecture" ,
655+ input : "pkg-name-1.2.3-4.azl3.rpm" ,
656+ expected : "pkg-name-1.2.3-4.azl3.rpm" ,
657+ },
658+ {
659+ name : "package with version containing the '+' character" ,
660+ input : "pkg-name-1.2.3+4-4.azl3.x86_64.rpm" ,
661+ expected : "pkg-name-1.2.3+4-4.azl3.x86_64.rpm" ,
662+ },
663+ {
664+ name : "package with version containing the '~' character" ,
665+ input : "pkg-name-1.2.3~4-4.azl3.x86_64.rpm" ,
666+ expected : "pkg-name-1.2.3~4-4.azl3.x86_64.rpm" ,
667+ },
668+ {
669+ name : "package with release containing two '.' characters" ,
670+ input : "pkg-name-1.2.3-4.5.azl3.x86_64.rpm" ,
671+ expected : "pkg-name-1.2.3-4.5.azl3.x86_64.rpm" ,
672+ },
673+ {
674+ name : "package with release containing the '_' character" ,
675+ input : "pkg-name-1.2.3-4_5.azl3.x86_64.rpm" ,
676+ expected : "pkg-name-1.2.3-4_5.azl3.x86_64.rpm" ,
677+ },
678+ {
679+ name : "package with release containing the `~` character" ,
680+ input : "pkg-name-1.2.3-4~5.azl3.x86_64.rpm" ,
681+ expected : "pkg-name-1.2.3-4~5.azl3.x86_64.rpm" ,
682+ },
683+ }
684+
685+ for _ , tt := range tests {
686+ t .Run (tt .name , func (t * testing.T ) {
687+ actual := StripEpochFromPackageFullQualifiedName (tt .input )
688+ assert .Equal (t , tt .expected , actual )
689+ })
690+ }
691+ }
692+
693+ func TestStripEpochFromPackageFullQualifiedNameWithInvalidInput (t * testing.T ) {
694+ tests := []struct {
695+ name string
696+ input string
697+ expected string
698+ }{
699+ {
700+ name : "invalid package name" ,
701+ input : "invalid-package-name" ,
702+ expected : "invalid-package-name" ,
703+ },
704+ {
705+ name : "empty package name" ,
706+ input : "" ,
707+ expected : "" ,
708+ },
709+ {
710+ name : "package name with only hyphens" ,
711+ input : "----" ,
712+ expected : "----" ,
713+ },
714+ {
715+ name : "package name with spaces" ,
716+ input : "pkg name-1.2.3-4.azl3.x86_64.rpm" ,
717+ expected : "pkg name-1.2.3-4.azl3.x86_64.rpm" ,
718+ },
719+ }
720+
721+ for _ , tt := range tests {
722+ t .Run (tt .name , func (t * testing.T ) {
723+ actual := StripEpochFromPackageFullQualifiedName (tt .input )
724+ assert .Equal (t , tt .expected , actual )
725+ })
726+ }
727+ }
0 commit comments