Skip to content

Commit 6aedba3

Browse files
authored
Merge pull request cli#3666 from cli/fix-windows-config
Fix creating Windows directory for gh config
2 parents dee89a1 + e0e25c8 commit 6aedba3

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

internal/config/config_file.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package config
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"io/ioutil"
87
"os"
9-
"path"
108
"path/filepath"
119
"syscall"
1210

@@ -111,7 +109,7 @@ var ReadConfigFile = func(filename string) ([]byte, error) {
111109
}
112110

113111
var WriteConfigFile = func(filename string, data []byte) error {
114-
err := os.MkdirAll(path.Dir(filename), 0771)
112+
err := os.MkdirAll(filepath.Dir(filename), 0771)
115113
if err != nil {
116114
return pathError(err)
117115
}
@@ -122,11 +120,7 @@ var WriteConfigFile = func(filename string, data []byte) error {
122120
}
123121
defer cfgFile.Close()
124122

125-
n, err := cfgFile.Write(data)
126-
if err == nil && n < len(data) {
127-
err = io.ErrShortWrite
128-
}
129-
123+
_, err = cfgFile.Write(data)
130124
return err
131125
}
132126

@@ -263,7 +257,7 @@ func findRegularFile(p string) string {
263257
if s, err := os.Stat(p); err == nil && s.Mode().IsRegular() {
264258
return p
265259
}
266-
newPath := path.Dir(p)
260+
newPath := filepath.Dir(p)
267261
if newPath == p || newPath == "/" || newPath == "." {
268262
break
269263
}

internal/config/config_file_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package config
33
import (
44
"bytes"
55
"fmt"
6+
"io/ioutil"
67
"os"
8+
"path/filepath"
79
"testing"
810

911
"github.com/stretchr/testify/assert"
@@ -167,3 +169,28 @@ func Test_ConfigDir(t *testing.T) {
167169
})
168170
}
169171
}
172+
173+
func Test_configFile_Write_toDisk(t *testing.T) {
174+
configDir := filepath.Join(t.TempDir(), ".config", "gh")
175+
os.Setenv(GH_CONFIG_DIR, configDir)
176+
defer os.Unsetenv(GH_CONFIG_DIR)
177+
178+
cfg := NewFromString(`pager: less`)
179+
err := cfg.Write()
180+
if err != nil {
181+
t.Fatal(err)
182+
}
183+
184+
expectedConfig := "pager: less\n"
185+
if configBytes, err := ioutil.ReadFile(filepath.Join(configDir, "config.yml")); err != nil {
186+
t.Error(err)
187+
} else if string(configBytes) != expectedConfig {
188+
t.Errorf("expected config.yml %q, got %q", expectedConfig, string(configBytes))
189+
}
190+
191+
if configBytes, err := ioutil.ReadFile(filepath.Join(configDir, "hosts.yml")); err != nil {
192+
t.Error(err)
193+
} else if string(configBytes) != "" {
194+
t.Errorf("unexpected hosts.yml: %q", string(configBytes))
195+
}
196+
}

0 commit comments

Comments
 (0)