Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 203f665

Browse files
committed
Add Windows specific function for making a file unreadable during tests.
1 parent 7948c8a commit 203f665

File tree

3 files changed

+62
-15
lines changed

3 files changed

+62
-15
lines changed

analyzer_notwindows_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !windows
6+
7+
package dep
8+
9+
import (
10+
"io"
11+
"os"
12+
)
13+
14+
func makeUnreadable(path string) (io.Closer, error) {
15+
err := os.Chmod(path, 0222)
16+
if err != nil {
17+
return nil, err
18+
}
19+
return closer{}, nil
20+
}
21+
22+
type closer struct{}
23+
24+
func (closer) Close() error { return nil }

analyzer_test.go

+5-15
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
package dep
66

77
import (
8-
"os"
98
"path/filepath"
10-
"runtime"
119
"testing"
1210

1311
"github.com/golang/dep/test"
@@ -64,31 +62,23 @@ func TestAnalyzerDeriveManifestAndLockDoesNotExist(t *testing.T) {
6462
}
6563

6664
func TestAnalyzerDeriveManifestAndLockCannotOpen(t *testing.T) {
67-
if runtime.GOOS == "windows" {
68-
// TODO: find an implementation that works on Microsoft
69-
// Windows. Setting permissions works differently there.
70-
// os.Chmod(..., 0222) below is not enough for the file
71-
// to be write-only (unreadable), and os.Chmod(...,
72-
// 0000) returns an invalid argument error.
73-
t.Skip("skipping on windows")
74-
}
75-
7665
h := test.NewHelper(t)
7766
defer h.Cleanup()
7867

7968
h.TempDir("dep")
8069

81-
// Create an empty manifest file
70+
// Simulate an inaccessible manifest file.
8271
h.TempFile(filepath.Join("dep", ManifestName), "")
83-
84-
// Change its mode so that it cannot be read
85-
err := os.Chmod(filepath.Join(h.Path("dep"), ManifestName), 0222)
72+
closer, err := makeUnreadable(filepath.Join(h.Path("dep"), ManifestName))
8673
if err != nil {
8774
t.Fatal(err)
8875
}
76+
defer closer.Close()
8977

9078
a := Analyzer{}
9179

80+
// Verify that the solver rejects the manifest, rather than treating it as
81+
// offering no constraints.
9282
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
9383
if m != nil || l != nil || err == nil {
9484
t.Fatalf("expected manifest & lock to be nil, err to be not nil: m -> %#v l -> %#v err -> %#v", m, l, err)

analyzer_windows_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package dep
6+
7+
import (
8+
"io"
9+
"os"
10+
"syscall"
11+
)
12+
13+
// makeUnreadable opens the file at path in exclusive mode. A file opened in
14+
// exclusive mode cannot be opened again until the exclusive mode file handle
15+
// is closed.
16+
func makeUnreadable(path string) (io.Closer, error) {
17+
if len(path) == 0 {
18+
return nil, syscall.ERROR_FILE_NOT_FOUND
19+
}
20+
pathp, err := syscall.UTF16PtrFromString(path)
21+
if err != nil {
22+
return nil, err
23+
}
24+
access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
25+
sharemode := uint32(0) // no sharing == exclusive mode
26+
sa := (*syscall.SecurityAttributes)(nil)
27+
createmode := uint32(syscall.OPEN_EXISTING)
28+
h, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
29+
if err != nil {
30+
return nil, err
31+
}
32+
return os.NewFile(uintptr(h), path), nil
33+
}

0 commit comments

Comments
 (0)