@@ -595,6 +595,126 @@ func TestOnlyStrict(t *testing.T) {
595595 }
596596}
597597
598+ func TestOnlyOS (t * testing.T ) {
599+ for _ , tc := range []struct {
600+ platform string
601+ matches map [bool ][]string
602+ }{
603+ {
604+ platform : "linux/amd64" ,
605+ matches : map [bool ][]string {
606+ true : {
607+ "linux/amd64" ,
608+ "linux/arm64" ,
609+ "linux/arm/v7" ,
610+ "linux/386" ,
611+ },
612+ false : {
613+ "windows/amd64" ,
614+ "darwin/arm64" ,
615+ },
616+ },
617+ },
618+ {
619+ platform : "windows(10.0.17763)/amd64" ,
620+ matches : map [bool ][]string {
621+ true : {
622+ "windows/amd64" ,
623+ "windows/arm64" ,
624+ "windows(10.0.17763)/amd64" ,
625+ "windows(10.0.17763)/arm64" ,
626+ },
627+ false : {
628+ "linux/amd64" ,
629+ },
630+ },
631+ },
632+ {
633+ platform : "linux(+gpu)/amd64" ,
634+ matches : map [bool ][]string {
635+ true : {
636+ "linux/arm64" ,
637+ "linux(+gpu)/amd64" ,
638+ },
639+ false : {
640+ "windows/amd64" ,
641+ "linux(+gpu+simd)/amd64" ,
642+ },
643+ },
644+ },
645+ } {
646+ testcase := tc
647+ t .Run (testcase .platform , func (t * testing.T ) {
648+ p , err := Parse (testcase .platform )
649+ if err != nil {
650+ t .Fatal (err )
651+ }
652+ m := OnlyOS (p )
653+ for shouldMatch , platforms := range testcase .matches {
654+ for _ , matchPlatform := range platforms {
655+ mp , err := Parse (matchPlatform )
656+ if err != nil {
657+ t .Fatal (err )
658+ }
659+ if match := m .Match (mp ); shouldMatch != match {
660+ t .Errorf ("OnlyOS(%q).Match(%q) should return %v, but returns %v" , testcase .platform , matchPlatform , shouldMatch , match )
661+ }
662+ }
663+ }
664+ })
665+ }
666+ }
667+
668+ func TestOnlyOSLess (t * testing.T ) {
669+ for _ , tc := range []struct {
670+ platform string
671+ platforms []string
672+ expected []string
673+ }{
674+ {
675+ // Exact architecture match ranks first, others are unordered but before non-OS matches
676+ platform : "linux/amd64" ,
677+ platforms : []string {"linux/arm64" , "linux/386" , "linux/amd64" , "windows/amd64" },
678+ expected : []string {"linux/amd64" , "linux/arm64" , "linux/386" , "windows/amd64" },
679+ },
680+ {
681+ // Strict: only exact arch/variant match is preferred
682+ platform : "linux/arm64" ,
683+ platforms : []string {"linux/amd64" , "linux/arm/v7" , "linux/arm64" , "windows/arm64" },
684+ expected : []string {"linux/arm64" , "linux/amd64" , "linux/arm/v7" , "windows/arm64" },
685+ },
686+ {
687+ // Non-matching OS should always sort last
688+ platform : "linux/amd64" ,
689+ platforms : []string {"windows/amd64" , "darwin/amd64" , "linux/arm64" , "linux/amd64" },
690+ expected : []string {"linux/amd64" , "linux/arm64" , "windows/amd64" , "darwin/amd64" },
691+ },
692+ } {
693+ testcase := tc
694+ t .Run (testcase .platform , func (t * testing.T ) {
695+ p , err := Parse (testcase .platform )
696+ if err != nil {
697+ t .Fatal (err )
698+ }
699+ mc := OnlyOS (p )
700+ platforms , err := ParseAll (testcase .platforms )
701+ if err != nil {
702+ t .Fatal (err )
703+ }
704+ sort .Slice (platforms , func (i , j int ) bool {
705+ return mc .Less (platforms [i ], platforms [j ])
706+ })
707+ actual := make ([]string , len (platforms ))
708+ for i , ps := range platforms {
709+ actual [i ] = FormatAll (ps )
710+ }
711+ if ! reflect .DeepEqual (testcase .expected , actual ) {
712+ t .Errorf ("Wrong platform order:\n Expected: %#v\n Actual: %#v" , testcase .expected , actual )
713+ }
714+ })
715+ }
716+ }
717+
598718func TestCompareOSFeatures (t * testing.T ) {
599719 for _ , tc := range []struct {
600720 platform string
0 commit comments