github.com/cpuid/libcompose@v0.4.0/cli/docker/app/factory_test.go (about) 1 package app 2 3 import ( 4 "flag" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/docker/libcompose/project" 11 "github.com/urfave/cli" 12 ) 13 14 func TestProjectFactoryProjectNameIsNormalized(t *testing.T) { 15 projects := []struct { 16 name string 17 expected string 18 }{ 19 { 20 name: "example", 21 expected: "example", 22 }, 23 { 24 name: "example-test", 25 expected: "exampletest", 26 }, 27 { 28 name: "aW3Ird_Project_with_$$", 29 expected: "aw3irdprojectwith", 30 }, 31 } 32 33 tmpDir, err := ioutil.TempDir("", "project-factory-test") 34 if err != nil { 35 t.Fatal(err) 36 } 37 composeFile := filepath.Join(tmpDir, "docker-compose.yml") 38 ioutil.WriteFile(composeFile, []byte(`hello: 39 image: busybox`), 0700) 40 41 for _, projectCase := range projects { 42 globalSet := flag.NewFlagSet("test", 0) 43 // Set the project-name flag 44 globalSet.String("project-name", projectCase.name, "doc") 45 // Set the compose file flag 46 globalSet.Var(&cli.StringSlice{composeFile}, "file", "doc") 47 c := cli.NewContext(nil, globalSet, nil) 48 factory := &ProjectFactory{} 49 p, err := factory.Create(c) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 if p.(*project.Project).Name != projectCase.expected { 55 t.Fatalf("expected %s, got %s", projectCase.expected, p.(*project.Project).Name) 56 } 57 } 58 } 59 60 func TestProjectFactoryFileArgMayContainMultipleFiles(t *testing.T) { 61 sep := string(os.PathListSeparator) 62 fileCases := []struct { 63 requested []string 64 available []string 65 expected []string 66 }{ 67 { 68 requested: []string{}, 69 available: []string{"docker-compose.yml"}, 70 expected: []string{"docker-compose.yml"}, 71 }, 72 { 73 requested: []string{}, 74 available: []string{"docker-compose.yml", "docker-compose.override.yml"}, 75 expected: []string{"docker-compose.yml", "docker-compose.override.yml"}, 76 }, 77 { 78 requested: []string{"one.yml"}, 79 available: []string{"one.yml"}, 80 expected: []string{"one.yml"}, 81 }, 82 { 83 requested: []string{"one.yml"}, 84 available: []string{"docker-compose.yml", "one.yml"}, 85 expected: []string{"one.yml"}, 86 }, 87 { 88 requested: []string{"one.yml", "two.yml", "three.yml"}, 89 available: []string{"one.yml", "two.yml", "three.yml"}, 90 expected: []string{"one.yml", "two.yml", "three.yml"}, 91 }, 92 { 93 requested: []string{"one.yml" + sep + "two.yml" + sep + "three.yml"}, 94 available: []string{"one.yml", "two.yml", "three.yml"}, 95 expected: []string{"one.yml", "two.yml", "three.yml"}, 96 }, 97 { 98 requested: []string{"one.yml" + sep + "two.yml", "three.yml" + sep + "four.yml"}, 99 available: []string{"one.yml", "two.yml", "three.yml", "four.yml"}, 100 expected: []string{"one.yml", "two.yml", "three.yml", "four.yml"}, 101 }, 102 { 103 requested: []string{"one.yml", "two.yml" + sep + "three.yml"}, 104 available: []string{"one.yml", "two.yml", "three.yml"}, 105 expected: []string{"one.yml", "two.yml", "three.yml"}, 106 }, 107 } 108 109 for _, fileCase := range fileCases { 110 tmpDir, err := ioutil.TempDir("", "project-factory-test") 111 if err != nil { 112 t.Fatal(err) 113 } 114 defer os.RemoveAll(tmpDir) 115 if err = os.Chdir(tmpDir); err != nil { 116 t.Fatal(err) 117 } 118 119 for _, file := range fileCase.available { 120 ioutil.WriteFile(file, []byte(`hello: 121 image: busybox`), 0700) 122 } 123 globalSet := flag.NewFlagSet("test", 0) 124 // Set the project-name flag 125 globalSet.String("project-name", "example", "doc") 126 // Set the compose file flag 127 fcr := cli.StringSlice(fileCase.requested) 128 globalSet.Var(&fcr, "file", "doc") 129 c := cli.NewContext(nil, globalSet, nil) 130 factory := &ProjectFactory{} 131 p, err := factory.Create(c) 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 for i, v := range p.(*project.Project).Files { 137 if v != fileCase.expected[i] { 138 t.Fatalf("requested %s, available %s, expected %s, got %s", 139 fileCase.requested, fileCase.available, fileCase.expected, p.(*project.Project).Files) 140 } 141 } 142 } 143 }