Skip to content

Instantly share code, notes, and snippets.

@yujiorama
Created February 5, 2022 07:32
Show Gist options
  • Save yujiorama/d11ca02cef8e41fc7eba251310d4e44b to your computer and use it in GitHub Desktop.
Save yujiorama/d11ca02cef8e41fc7eba251310d4e44b to your computer and use it in GitHub Desktop.
file_operation_test.go
package main
import (
"os"
"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() {
_, 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