github.com/skippbox/kompose-origin@v0.0.0-20160524133224-16a9dca7bac2/cli/docker/app/factory_test.go (about)

     1  package app
     2  
     3  import (
     4  	"flag"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/codegangsta/cli"
    10  	"github.com/docker/libcompose/project"
    11  )
    12  
    13  func TestProjectFactoryProjectNameIsNormalized(t *testing.T) {
    14  	projects := []struct {
    15  		name     string
    16  		expected string
    17  	}{
    18  		{
    19  			name:     "example",
    20  			expected: "example",
    21  		},
    22  		{
    23  			name:     "example-test",
    24  			expected: "exampletest",
    25  		},
    26  		{
    27  			name:     "aW3Ird_Project_with_$$",
    28  			expected: "aw3irdprojectwith",
    29  		},
    30  	}
    31  
    32  	tmpDir, err := ioutil.TempDir("", "project-factory-test")
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	composeFile := filepath.Join(tmpDir, "docker-compose.yml")
    37  	ioutil.WriteFile(composeFile, []byte(`hello:
    38      image: busybox`), 0700)
    39  
    40  	for _, projectCase := range projects {
    41  		globalSet := flag.NewFlagSet("test", 0)
    42  		// Set the project-name flag
    43  		globalSet.String("project-name", projectCase.name, "doc")
    44  		// Set the compose file flag
    45  		globalSet.Var(&cli.StringSlice{composeFile}, "file", "doc")
    46  		c := cli.NewContext(nil, globalSet, nil)
    47  		factory := &ProjectFactory{}
    48  		p, err := factory.Create(c)
    49  		if err != nil {
    50  			t.Fatal(err)
    51  		}
    52  
    53  		if p.(*project.Project).Name != projectCase.expected {
    54  			t.Fatalf("expected %s, got %s", projectCase.expected, p.(*project.Project).Name)
    55  		}
    56  	}
    57  }