|
17 | 17 | package platforms |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "reflect" |
| 21 | + "sort" |
20 | 22 | "testing" |
21 | 23 | ) |
22 | 24 |
|
@@ -592,3 +594,94 @@ func TestOnlyStrict(t *testing.T) { |
592 | 594 | }) |
593 | 595 | } |
594 | 596 | } |
| 597 | + |
| 598 | +func TestCompareOSFeatures(t *testing.T) { |
| 599 | + for _, tc := range []struct { |
| 600 | + platform string |
| 601 | + platforms []string |
| 602 | + expected []string |
| 603 | + }{ |
| 604 | + { |
| 605 | + "linux/amd64", |
| 606 | + []string{"windows/amd64", "linux/amd64", "linux(+other)/amd64", "linux/arm64"}, |
| 607 | + []string{"linux/amd64", "linux(+other)/amd64", "windows/amd64", "linux/arm64"}, |
| 608 | + }, |
| 609 | + { |
| 610 | + "linux(+none)/amd64", |
| 611 | + []string{"windows/amd64", "linux/amd64", "linux/arm64", "linux(+other)/amd64"}, |
| 612 | + []string{"linux/amd64", "linux(+other)/amd64", "windows/amd64", "linux/arm64"}, |
| 613 | + }, |
| 614 | + { |
| 615 | + "linux(+other)/amd64", |
| 616 | + []string{"windows/amd64", "linux/amd64", "linux/arm64", "linux(+other)/amd64"}, |
| 617 | + []string{"linux(+other)/amd64", "linux/amd64", "windows/amd64", "linux/arm64"}, |
| 618 | + }, |
| 619 | + { |
| 620 | + "linux(+af+other+zf)/amd64", |
| 621 | + []string{"windows/amd64", "linux/amd64", "linux/arm64", "linux(+other)/amd64"}, |
| 622 | + []string{"linux(+other)/amd64", "linux/amd64", "windows/amd64", "linux/arm64"}, |
| 623 | + }, |
| 624 | + { |
| 625 | + "linux(+f1+f2)/amd64", |
| 626 | + []string{"linux/amd64", "linux(+f2)/amd64", "linux(+f1)/amd64", "linux(+f1+f2)/amd64"}, |
| 627 | + []string{"linux(+f1+f2)/amd64", "linux(+f2)/amd64", "linux(+f1)/amd64", "linux/amd64"}, |
| 628 | + }, |
| 629 | + { |
| 630 | + // This test should likely fail and be updated when os version is considered for linux |
| 631 | + "linux(7.2+other)/amd64", |
| 632 | + []string{"linux/amd64", "linux(+other)/amd64", "linux(7.1)/amd64", "linux(7.2+other)/amd64"}, |
| 633 | + []string{"linux(+other)/amd64", "linux(7.2+other)/amd64", "linux/amd64", "linux(7.1)/amd64"}, |
| 634 | + }, |
| 635 | + } { |
| 636 | + testcase := tc |
| 637 | + t.Run(testcase.platform, func(t *testing.T) { |
| 638 | + t.Parallel() |
| 639 | + p, err := Parse(testcase.platform) |
| 640 | + if err != nil { |
| 641 | + t.Fatal(err) |
| 642 | + } |
| 643 | + |
| 644 | + for _, stc := range []struct { |
| 645 | + name string |
| 646 | + mc MatchComparer |
| 647 | + }{ |
| 648 | + { |
| 649 | + name: "only", |
| 650 | + mc: Only(p), |
| 651 | + }, |
| 652 | + { |
| 653 | + name: "only strict", |
| 654 | + mc: OnlyStrict(p), |
| 655 | + }, |
| 656 | + { |
| 657 | + name: "ordered", |
| 658 | + mc: Ordered(p), |
| 659 | + }, |
| 660 | + { |
| 661 | + name: "any", |
| 662 | + mc: Any(p), |
| 663 | + }, |
| 664 | + } { |
| 665 | + mc := stc.mc |
| 666 | + testcase := testcase |
| 667 | + t.Run(stc.name, func(t *testing.T) { |
| 668 | + p, err := ParseAll(testcase.platforms) |
| 669 | + if err != nil { |
| 670 | + t.Fatal(err) |
| 671 | + } |
| 672 | + sort.Slice(p, func(i, j int) bool { |
| 673 | + return mc.Less(p[i], p[j]) |
| 674 | + }) |
| 675 | + actual := make([]string, len(p)) |
| 676 | + for i, ps := range p { |
| 677 | + actual[i] = FormatAll(ps) |
| 678 | + } |
| 679 | + |
| 680 | + if !reflect.DeepEqual(testcase.expected, actual) { |
| 681 | + t.Errorf("Wrong platform order:\nExpected: %#v\nActual: %#v", testcase.expected, actual) |
| 682 | + } |
| 683 | + }) |
| 684 | + } |
| 685 | + }) |
| 686 | + } |
| 687 | +} |
0 commit comments