github.com/jdhenke/godel@v0.0.0-20161213181855-abeb3861bf0d/cmd/idea/idea_test.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package idea_test
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"path"
    22  	"testing"
    23  
    24  	"github.com/nmiyake/pkg/dirs"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  
    28  	"github.com/palantir/godel/cmd/idea"
    29  )
    30  
    31  func TestCreateIdeaFiles(t *testing.T) {
    32  	tmpDir, cleanup, err := dirs.TempDir("", "")
    33  	defer cleanup()
    34  	require.NoError(t, err)
    35  
    36  	err = idea.CreateIdeaFiles(tmpDir)
    37  	assert.NoError(t, err)
    38  
    39  	verifyXMLHelper(t, ideaFilePath(tmpDir, "iml"))
    40  	verifyXMLHelper(t, ideaFilePath(tmpDir, "ipr"))
    41  }
    42  
    43  func TestCleanIdeaFiles(t *testing.T) {
    44  	tmpDir, cleanup, err := dirs.TempDir("", "")
    45  	defer cleanup()
    46  	require.NoError(t, err)
    47  
    48  	for i, currCase := range [][]string{
    49  		{"iml", "ipr", "iws"},
    50  		// test case where not all files to be cleaned exist
    51  		{"iml", "ipr"},
    52  	} {
    53  		currDir, err := ioutil.TempDir(tmpDir, "")
    54  		require.NoError(t, err)
    55  
    56  		for _, ext := range currCase {
    57  			currPath := ideaFilePath(currDir, ext)
    58  			err := ioutil.WriteFile(currPath, []byte(ext), 0644)
    59  			require.NoError(t, err, "Case %d: failed to write %v", i, currPath)
    60  		}
    61  
    62  		err = idea.CleanIdeaFiles(currDir)
    63  		require.NoError(t, err)
    64  
    65  		for _, ext := range currCase {
    66  			currPath := ideaFilePath(currDir, ext)
    67  			_, err = os.Stat(currPath)
    68  			assert.True(t, os.IsNotExist(err), "Case %d: did not expect %v to exist", i, currPath)
    69  		}
    70  	}
    71  }
    72  
    73  func verifyXMLHelper(t *testing.T, fPath string) {
    74  	fInfo, err := os.Stat(fPath)
    75  	assert.NoError(t, err)
    76  	assert.False(t, fInfo.IsDir())
    77  }
    78  
    79  func ideaFilePath(dir, ext string) string {
    80  	return path.Join(dir, fmt.Sprintf("%v.%v", path.Base(dir), ext))
    81  }