Add Windows specific function for making a file unreadable during tests.

This commit is contained in:
Chris Hines 2017-05-02 02:22:44 -04:00
Родитель 7948c8a848
Коммит 203f665f53
3 изменённых файлов: 62 добавлений и 15 удалений

Просмотреть файл

@ -0,0 +1,24 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !windows
package dep
import (
"io"
"os"
)
func makeUnreadable(path string) (io.Closer, error) {
err := os.Chmod(path, 0222)
if err != nil {
return nil, err
}
return closer{}, nil
}
type closer struct{}
func (closer) Close() error { return nil }

Просмотреть файл

@ -5,9 +5,7 @@
package dep
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/golang/dep/test"
@ -64,31 +62,23 @@ func TestAnalyzerDeriveManifestAndLockDoesNotExist(t *testing.T) {
}
func TestAnalyzerDeriveManifestAndLockCannotOpen(t *testing.T) {
if runtime.GOOS == "windows" {
// TODO: find an implementation that works on Microsoft
// Windows. Setting permissions works differently there.
// os.Chmod(..., 0222) below is not enough for the file
// to be write-only (unreadable), and os.Chmod(...,
// 0000) returns an invalid argument error.
t.Skip("skipping on windows")
}
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("dep")
// Create an empty manifest file
// Simulate an inaccessible manifest file.
h.TempFile(filepath.Join("dep", ManifestName), "")
// Change its mode so that it cannot be read
err := os.Chmod(filepath.Join(h.Path("dep"), ManifestName), 0222)
closer, err := makeUnreadable(filepath.Join(h.Path("dep"), ManifestName))
if err != nil {
t.Fatal(err)
}
defer closer.Close()
a := Analyzer{}
// Verify that the solver rejects the manifest, rather than treating it as
// offering no constraints.
m, l, err := a.DeriveManifestAndLock(h.Path("dep"), "my/fake/project")
if m != nil || l != nil || err == nil {
t.Fatalf("expected manifest & lock to be nil, err to be not nil: m -> %#v l -> %#v err -> %#v", m, l, err)

33
analyzer_windows_test.go Normal file
Просмотреть файл

@ -0,0 +1,33 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dep
import (
"io"
"os"
"syscall"
)
// makeUnreadable opens the file at path in exclusive mode. A file opened in
// exclusive mode cannot be opened again until the exclusive mode file handle
// is closed.
func makeUnreadable(path string) (io.Closer, error) {
if len(path) == 0 {
return nil, syscall.ERROR_FILE_NOT_FOUND
}
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return nil, err
}
access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
sharemode := uint32(0) // no sharing == exclusive mode
sa := (*syscall.SecurityAttributes)(nil)
createmode := uint32(syscall.OPEN_EXISTING)
h, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(h), path), nil
}