github.com/rumpl/mod@v1.1.0/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"testing"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  type MainSuite struct {
    16  	tmpDir string
    17  	suite.Suite
    18  }
    19  
    20  func (s *MainSuite) BeforeTest(suite, test string) {
    21  	logrus.SetLevel(logrus.DebugLevel)
    22  	dir, err := ioutil.TempDir("", "test")
    23  	require.Nil(s.T(), err)
    24  
    25  	s.tmpDir = dir
    26  
    27  	err = os.Chdir(s.tmpDir)
    28  	require.Nil(s.T(), err)
    29  }
    30  
    31  func (s *MainSuite) AfterTest(suite, test string) {
    32  	err := os.RemoveAll(s.tmpDir)
    33  	require.Nil(s.T(), err)
    34  }
    35  
    36  func (s *MainSuite) TestInit() {
    37  	dir := "repo"
    38  
    39  	err := run(dir)
    40  	require.Nil(s.T(), err)
    41  
    42  	if _, err := os.Stat(path.Join(s.tmpDir, dir)); os.IsNotExist(err) {
    43  		s.Fail("Did not create repo dir")
    44  	}
    45  
    46  	if _, err := os.Stat(path.Join(s.tmpDir, dir, "README.md")); os.IsNotExist(err) {
    47  		s.Fail("Did not create the README file")
    48  	}
    49  }
    50  
    51  func (s *MainSuite) TestInitDirectoryExists() {
    52  	dir := "repo"
    53  
    54  	err := os.Mkdir(path.Join(s.tmpDir, dir), 0755)
    55  	require.Nil(s.T(), err)
    56  
    57  	err = run(dir)
    58  	assert.Errorf(s.T(), err, "directory already exists")
    59  }
    60  
    61  func TestMainSuite(t *testing.T) {
    62  	suite.Run(t, new(MainSuite))
    63  }