github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/db/migration/cli/command/command_test.go (about)

     1  package cli_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"regexp"
     8  	"strings"
     9  
    10  	cmd "github.com/pf-qiu/concourse/v6/atc/db/migration/cli/command"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Migration CLI", func() {
    16  
    17  	Context("generate migration files", func() {
    18  		var (
    19  			migrationDir  string
    20  			command       *cmd.GenerateCommand
    21  			migrationName = "create_new_table"
    22  			err           error
    23  		)
    24  
    25  		BeforeEach(func() {
    26  			migrationDir, err = ioutil.TempDir("", "")
    27  			Expect(err).ToNot(HaveOccurred())
    28  
    29  		})
    30  
    31  		AfterEach(func() {
    32  			os.RemoveAll(migrationDir)
    33  		})
    34  
    35  		Context("sql migrations", func() {
    36  
    37  			It("generates up and down migration files", func() {
    38  				command = cmd.NewGenerateCommand(migrationDir, migrationName, "sql")
    39  				sqlMigrationPattern := "^(\\d+)_(.*).(down|up).sql$"
    40  
    41  				err := command.GenerateSQLMigration()
    42  				Expect(err).ToNot(HaveOccurred())
    43  
    44  				ExpectGeneratedFilesToMatchSpecification(migrationDir, sqlMigrationPattern,
    45  					migrationName, func(migrationID string, actualFileContents string) {
    46  						Expect(actualFileContents).To(BeEmpty())
    47  					})
    48  			})
    49  		})
    50  
    51  		Context("go migration", func() {
    52  			It("generates up and down migration files", func() {
    53  				command = cmd.NewGenerateCommand(migrationDir, migrationName, "go")
    54  				goMigrationPattern := "^(\\d+)_(.*).(down|up).go$"
    55  
    56  				err := command.GenerateGoMigration()
    57  				Expect(err).ToNot(HaveOccurred())
    58  
    59  				ExpectGeneratedFilesToMatchSpecification(migrationDir, goMigrationPattern,
    60  					migrationName, func(migrationID string, actualFileContents string) {
    61  						lines := strings.Split(actualFileContents, "\n")
    62  						Expect(lines).To(HaveLen(6))
    63  
    64  						Expect(lines[0]).To(Equal("package migrations"))
    65  						Expect(lines[1]).To(Equal(""))
    66  						Expect(lines[2]).To(HavePrefix("//"))
    67  						Expect(lines[3]).To(MatchRegexp("^func (Up|Down)_%s\\(\\) error {", migrationID))
    68  						Expect(lines[4]).To(ContainSubstring("return nil"))
    69  						Expect(lines[5]).To(Equal("}"))
    70  					})
    71  			})
    72  
    73  		})
    74  	})
    75  })
    76  
    77  func ExpectGeneratedFilesToMatchSpecification(migrationDir, fileNamePattern, migrationName string,
    78  	checkContents func(migrationID string, actualFileContents string)) {
    79  
    80  	files, err := ioutil.ReadDir(migrationDir)
    81  	Expect(err).ToNot(HaveOccurred())
    82  	var migrationFilesCount = 0
    83  	regex := regexp.MustCompile(fileNamePattern)
    84  	for _, migrationFile := range files {
    85  		var matches []string
    86  		migrationFileName := migrationFile.Name()
    87  		if regex.MatchString(migrationFileName) {
    88  			matches = regex.FindStringSubmatch(migrationFileName)
    89  
    90  			Expect(matches).To(HaveLen(4))
    91  			Expect(matches[2]).To(Equal(migrationName))
    92  
    93  			fileContents, err := ioutil.ReadFile(path.Join(migrationDir, migrationFileName))
    94  			Expect(err).ToNot(HaveOccurred())
    95  			checkContents(matches[1], string(fileContents))
    96  			migrationFilesCount++
    97  		}
    98  	}
    99  	Expect(migrationFilesCount).To(Equal(2))
   100  }