Skip to content

Commit

Permalink
fix volume case issue in windows platform (hashicorp#199)
Browse files Browse the repository at this point in the history
* fix volume case issue in windows platform

* update

* add test case
  • Loading branch information
njuCZ authored Jul 2, 2020
1 parent 33b7155 commit 9df6b79
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 6 deletions.
12 changes: 12 additions & 0 deletions internal/terraform/rootmodule/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package rootmodule

import (
"path/filepath"
"strings"
)

func pathEquals(path1, path2 string) bool {
volume1 := filepath.VolumeName(path1)
volume2 := filepath.VolumeName(path2)
return strings.EqualFold(volume1, volume2) && path1[len(volume1):] == path2[len(volume2):]
}
39 changes: 39 additions & 0 deletions internal/terraform/rootmodule/path_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// +build !windows

package rootmodule

import (
"fmt"
"testing"
)

func TestPathEquals(t *testing.T) {
testCases := []struct {
name string
path1 string
path2 string
expected bool
}{
{
"path the same",
`/home/user/documents/tf`,
`/home/user/documents/tf`,
true,
},
{
"path case not the same",
`/home/user/documents/tf`,
`/Home/user/documents/tf`,
false,
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
result := pathEquals(tc.path1, tc.path2)
if result != tc.expected {
t.Fatalf("expected: %t Got: %t", tc.expected, result)
}
})
}
}
49 changes: 49 additions & 0 deletions internal/terraform/rootmodule/path_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package rootmodule

import (
"fmt"
"testing"
)

func TestPathEquals(t *testing.T) {
testCases := []struct {
name string
path1 string
path2 string
expected bool
}{
{
"file path the same",
`c:\Users\user\Documents\tf`,
`c:\Users\user\Documents\tf`,
true,
},
{
"volume case insensitive",
`c:\Users\user\Documents\tf`,
`C:\Users\user\Documents\tf`,
true,
},
{
"path folder case different",
`c:\Users\user\Documents\tf`,
`c:\Users\user\documents\tf`,
false,
},
{
"file path different",
`c:\Users\user\Documents\tf`,
`c:\Users\user\Documents\tf\test`,
false,
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
result := pathEquals(tc.path1, tc.path2)
if result != tc.expected {
t.Fatalf("expected: %t Got: %t", tc.expected, result)
}
})
}
}
8 changes: 4 additions & 4 deletions internal/terraform/rootmodule/root_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ func (rm *rootModule) ReferencesModulePath(path string) bool {
continue
}
absPath := filepath.Join(rm.moduleManifest.rootDir, m.Dir)
rm.logger.Printf("checking if %q == %q", absPath, path)
if absPath == path {
rm.logger.Printf("checking if %q equals %q", absPath, path)
if pathEquals(absPath, path) {
return true
}
}
Expand Down Expand Up @@ -310,7 +310,7 @@ func (rm *rootModule) IsKnownModuleManifestFile(path string) bool {
return false
}

return rm.moduleManifestFile.Path() == path
return pathEquals(rm.moduleManifestFile.Path(), path)
}

func (rm *rootModule) IsKnownPluginLockFile(path string) bool {
Expand All @@ -321,5 +321,5 @@ func (rm *rootModule) IsKnownPluginLockFile(path string) bool {
return false
}

return rm.pluginLockFile.Path() == path
return pathEquals(rm.pluginLockFile.Path(), path)
}
4 changes: 2 additions & 2 deletions internal/terraform/rootmodule/root_module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (rmm *rootModuleManager) AddRootModule(dir string) error {

func (rmm *rootModuleManager) exists(dir string) bool {
for _, rm := range rmm.rms {
if rm.Path() == dir {
if pathEquals(rm.Path(), dir) {
return true
}
}
Expand All @@ -101,7 +101,7 @@ func (rmm *rootModuleManager) exists(dir string) bool {

func (rmm *rootModuleManager) rootModuleByPath(dir string) *rootModule {
for _, rm := range rmm.rms {
if rm.Path() == dir {
if pathEquals(rm.Path(), dir) {
return rm
}
}
Expand Down

0 comments on commit 9df6b79

Please sign in to comment.