github.com/gopinath-langote/1build@v1.7.0/testing/fixtures/command_delete_fixtures.go (about)

     1  package fixtures
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/gopinath-langote/1build/testing/def"
     8  	"github.com/gopinath-langote/1build/testing/utils"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func featureDeleteTestData() []Test {
    13  	feature := "delete"
    14  	return []Test{
    15  		shouldDeleteConfigFile(feature),
    16  		shouldDeleteConfigSpecifiedFile(feature),
    17  		shouldFailIfFileDoesntExists(feature, ""),
    18  		shouldFailIfFileDoesntExists(feature, "--force"),
    19  	}
    20  }
    21  
    22  func shouldDeleteConfigFile(feature string) Test {
    23  	return Test{
    24  		Feature: feature,
    25  		Name:    "shouldDeleteConfigFile",
    26  		CmdArgs: Args("delete", "--force"),
    27  		Setup: func(dir string) error {
    28  			return utils.CreateConfigFile(dir, "project: Sample Project\ncommands:\n")
    29  		},
    30  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    31  			return assertFileNotExists(t, dir+"/"+def.ConfigFileName)
    32  		},
    33  	}
    34  }
    35  
    36  func shouldDeleteConfigSpecifiedFile(feature string) Test {
    37  	return Test{
    38  		Feature: feature,
    39  		Name:    "shouldDeleteConfigSpecifiedFile",
    40  		CmdArgs: func(dir string) []string {
    41  			return []string{"delete", "-f", dir + "/custom-directory/some-file.yaml", "--force"}
    42  		},
    43  		Setup: func(dir string) error {
    44  			_ = os.MkdirAll(dir+"/custom-directory", 0750)
    45  			return utils.CreateConfigFileWithName(
    46  				dir+"/custom-directory", "some-file.yaml", "project: Sample Project\ncommands:\n")
    47  		},
    48  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    49  			return assertFileNotExists(t, dir+"/custom-directory/some-file.yaml")
    50  		},
    51  	}
    52  }
    53  
    54  func shouldFailIfFileDoesntExists(feature string, arg string) Test {
    55  	expectedOutput := "No configuration file found!"
    56  	return Test{
    57  		Feature: feature,
    58  		Name:    "shouldFailIfFileDoesntExists",
    59  		CmdArgs: Args("delete", arg),
    60  		Assertion: func(dir string, actualOutput string, t *testing.T) bool {
    61  			return assert.Contains(t, actualOutput, expectedOutput)
    62  		},
    63  	}
    64  }
    65  
    66  func assertFileNotExists(t *testing.T, path string) bool {
    67  	_, err := os.Stat(path)
    68  	if err == nil {
    69  		assert.Fail(t, "Delete command did not delete config file!")
    70  		return false
    71  	} else if !os.IsNotExist(err) {
    72  		assert.Fail(t, "error running os.Stat(%q): %s", path, err)
    73  		return false
    74  	}
    75  	return true
    76  }