Skip to content

Instantly share code, notes, and snippets.

@yujiorama
Created February 5, 2022 07:33
Show Gist options
  • Save yujiorama/acec2f9987e235692ec6b71a456f4f41 to your computer and use it in GitHub Desktop.
Save yujiorama/acec2f9987e235692ec6b71a456f4f41 to your computer and use it in GitHub Desktop.
file_operation_test2.go
package main
import (
"os"
"runtime"
"strings"
"testing"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testFileOperation(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
path string
)
it.Before(func() {
file, err := os.CreateTemp("", "test.txt")
Expect(err).NotTo(HaveOccurred())
defer file.Close()
path = file.Name()
})
it.After(func() {
Expect(os.RemoveAll(path)).To(Succeed())
})
context("when test.txt cannot be opened", func() {
it.Before(func() {
Expect(os.Chmod(path, 0000)).To(Succeed())
})
it("returns an error", func() {
if strings.Compare("windows", runtime.GOOS) == 0 {
t.SkipNow()
}
_, err := parser.Parse(path)
Expect(err).To(MatchError(ContainSubstring("failed to parse cpanfile.snapshot:")))
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment