github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/integration_test/integration_symlinks_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 integration_test
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"os"
    21  	"os/exec"
    22  	"path"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/nmiyake/pkg/dirs"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  // * Symlink "test-go" -> $GOPATH
    33  // * Set current directory to test project inside the symlink
    34  // * Verify that "./godelw check" works in sym-linked path
    35  func TestCheckInGoPathSymLink(t *testing.T) {
    36  	testProjectDir := setUpGödelTestAndDownload(t, testRootDir, gödelTGZ, version)
    37  	src := `package foo_test
    38  	import "testing"
    39  
    40  	func TestFoo(t *testing.T) {}`
    41  	err := ioutil.WriteFile(path.Join(testProjectDir, "foo_test.go"), []byte(src), 0644)
    42  	require.NoError(t, err)
    43  
    44  	symLinkParentDir, cleanup, err := dirs.TempDir("", "")
    45  	defer cleanup()
    46  	require.NoError(t, err)
    47  	symLinkPath := path.Join(symLinkParentDir, "test-go")
    48  
    49  	originalGoPath := os.Getenv("GOPATH")
    50  	err = os.Symlink(originalGoPath, symLinkPath)
    51  	require.NoError(t, err)
    52  
    53  	testProjectRelPath, err := filepath.Rel(originalGoPath, testProjectDir)
    54  	require.NoError(t, err)
    55  
    56  	// use script to set cd because setting wd on exec.Command does not work for symlinks
    57  	projectPathInSymLink := path.Join(symLinkPath, testProjectRelPath)
    58  	scriptTemplate := `#!/bin/bash
    59  cd %v
    60  pwd
    61  `
    62  	scriptFilePath := path.Join(symLinkParentDir, "script.sh")
    63  	err = ioutil.WriteFile(scriptFilePath, []byte(fmt.Sprintf(scriptTemplate, projectPathInSymLink)), 0755)
    64  	require.NoError(t, err)
    65  
    66  	cmd := exec.Command(scriptFilePath)
    67  	output, err := cmd.CombinedOutput()
    68  	require.NoError(t, err, "Command %v failed. Output:\n%v", cmd.Args, string(output))
    69  	assert.Equal(t, projectPathInSymLink, strings.TrimSpace(string(output)))
    70  
    71  	scriptTemplate = `#!/bin/bash
    72  cd %v
    73  ./godelw check
    74  `
    75  	err = ioutil.WriteFile(scriptFilePath, []byte(fmt.Sprintf(scriptTemplate, projectPathInSymLink)), 0755)
    76  	require.NoError(t, err)
    77  
    78  	cmd = exec.Command(scriptFilePath)
    79  	output, err = cmd.CombinedOutput()
    80  	require.NoError(t, err, "Command %v failed. Output:\n%v", cmd.Args, string(output))
    81  }
    82  
    83  // * Symlink "test-go" -> $GOPATH
    84  // * Set $GOPATH to be the symlink ("test-go")
    85  // * Set current directory to test project inside the symlink
    86  // * Verify that "./godelw check" works in sym-linked path
    87  // * Restore $GOPATH to original value
    88  func TestCheckInGoPathSymLinkGoPathSymLink(t *testing.T) {
    89  	testProjectDir := setUpGödelTestAndDownload(t, testRootDir, gödelTGZ, version)
    90  	src := `package foo_test
    91  	import "testing"
    92  
    93  	func TestFoo(t *testing.T) {}`
    94  	err := ioutil.WriteFile(path.Join(testProjectDir, "foo_test.go"), []byte(src), 0644)
    95  	require.NoError(t, err)
    96  
    97  	symLinkParentDir, cleanup, err := dirs.TempDir("", "")
    98  	defer cleanup()
    99  	require.NoError(t, err)
   100  	symLinkPath := path.Join(symLinkParentDir, "test-go")
   101  
   102  	originalGoPath := os.Getenv("GOPATH")
   103  	err = os.Symlink(originalGoPath, symLinkPath)
   104  	require.NoError(t, err)
   105  
   106  	err = os.Setenv("GOPATH", symLinkPath)
   107  	require.NoError(t, err)
   108  	defer func() {
   109  		if err := os.Setenv("GOPATH", originalGoPath); err != nil {
   110  			require.NoError(t, err, "failed to restore GOPATH environment variable in defer")
   111  		}
   112  	}()
   113  
   114  	testProjectRelPath, err := filepath.Rel(originalGoPath, testProjectDir)
   115  	require.NoError(t, err)
   116  
   117  	// use script to set cd because setting wd on exec.Command does not work for symlinks
   118  	projectPathInSymLink := path.Join(symLinkPath, testProjectRelPath)
   119  	scriptTemplate := `#!/bin/bash
   120  cd %v
   121  pwd
   122  `
   123  	scriptFilePath := path.Join(symLinkParentDir, "script.sh")
   124  	err = ioutil.WriteFile(scriptFilePath, []byte(fmt.Sprintf(scriptTemplate, projectPathInSymLink)), 0755)
   125  	require.NoError(t, err)
   126  
   127  	cmd := exec.Command(scriptFilePath)
   128  	output, err := cmd.CombinedOutput()
   129  	require.NoError(t, err, "Command %v failed. Output:\n%v", cmd.Args, string(output))
   130  	assert.Equal(t, projectPathInSymLink, strings.TrimSpace(string(output)))
   131  
   132  	scriptTemplate = `#!/bin/bash
   133  cd %v
   134  ./godelw check
   135  `
   136  	err = ioutil.WriteFile(scriptFilePath, []byte(fmt.Sprintf(scriptTemplate, projectPathInSymLink)), 0755)
   137  	require.NoError(t, err)
   138  
   139  	cmd = exec.Command(scriptFilePath)
   140  	output, err = cmd.CombinedOutput()
   141  	require.NoError(t, err, "Command %v failed. Output:\n%v", cmd.Args, string(output))
   142  }
   143  
   144  // * Symlink "test-go" -> $GOPATH
   145  // * Set $GOPATH to be the symlink ("test-go")
   146  // * Set current directory to real project (not inside symlink)
   147  // * Verify that "./godelw check" works in real path
   148  // * Restore $GOPATH to original value
   149  func TestCheckInGoPathNonSymLinkWhenGoPathIsSymLink(t *testing.T) {
   150  	testProjectDir := setUpGödelTestAndDownload(t, testRootDir, gödelTGZ, version)
   151  	src := `package foo_test
   152  	import "testing"
   153  
   154  	func TestFoo(t *testing.T) {}`
   155  	err := ioutil.WriteFile(path.Join(testProjectDir, "foo_test.go"), []byte(src), 0644)
   156  	require.NoError(t, err)
   157  
   158  	symLinkParentDir, cleanup, err := dirs.TempDir("", "")
   159  	defer cleanup()
   160  	require.NoError(t, err)
   161  	symLinkPath := path.Join(symLinkParentDir, "test-go")
   162  
   163  	originalGoPath := os.Getenv("GOPATH")
   164  	err = os.Symlink(originalGoPath, symLinkPath)
   165  	require.NoError(t, err)
   166  
   167  	err = os.Setenv("GOPATH", symLinkPath)
   168  	require.NoError(t, err)
   169  	defer func() {
   170  		if err := os.Setenv("GOPATH", originalGoPath); err != nil {
   171  			require.NoError(t, err, "failed to restore GOPATH environment variable in defer")
   172  		}
   173  	}()
   174  
   175  	cmd := exec.Command("./godelw", "check")
   176  	cmd.Dir = testProjectDir
   177  	output, err := cmd.CombinedOutput()
   178  	require.NoError(t, err, "Command %v failed. Output:\n%v", cmd.Args, string(output))
   179  }