|
| 1 | +package pipenvinstall_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + pipenvinstall "github.com/paketo-community/pipenv-install" |
| 11 | + "github.com/sclevine/spec" |
| 12 | +) |
| 13 | + |
| 14 | +func testPipfileParser(t *testing.T, context spec.G, it spec.S) { |
| 15 | + var ( |
| 16 | + Expect = NewWithT(t).Expect |
| 17 | + |
| 18 | + workingDir string |
| 19 | + parser pipenvinstall.PipfileParser |
| 20 | + ) |
| 21 | + |
| 22 | + it.Before(func() { |
| 23 | + var err error |
| 24 | + workingDir, err = ioutil.TempDir("", "working-dir") |
| 25 | + Expect(err).NotTo(HaveOccurred()) |
| 26 | + |
| 27 | + parser = pipenvinstall.NewPipfileParser() |
| 28 | + }) |
| 29 | + |
| 30 | + context("Calling ParseVersion", func() { |
| 31 | + context("when Pipfile is valid and specifies a CPython version", func() { |
| 32 | + |
| 33 | + it.Before(func() { |
| 34 | + Expect(ioutil.WriteFile( |
| 35 | + filepath.Join(workingDir, "Pipfile"), |
| 36 | + []byte(` |
| 37 | +[requires] |
| 38 | +python_version = "3.8" |
| 39 | +`), os.ModePerm)).To(Succeed()) |
| 40 | + }) |
| 41 | + |
| 42 | + it.After(func() { |
| 43 | + Expect(os.Remove(filepath.Join(workingDir, "Pipfile"))).To(Succeed()) |
| 44 | + }) |
| 45 | + |
| 46 | + it("parses the Python version", func() { |
| 47 | + version, err := parser.ParseVersion(workingDir) |
| 48 | + Expect(err).ToNot(HaveOccurred()) |
| 49 | + Expect(version).To(Equal("3.8")) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + context("failure cases", func() { |
| 54 | + context("when Pipfile file cannot be read", func() { |
| 55 | + it.Before(func() { |
| 56 | + Expect(ioutil.WriteFile( |
| 57 | + filepath.Join(workingDir, "Pipfile"), |
| 58 | + []byte(`{}`), os.ModePerm)).To(Succeed()) |
| 59 | + Expect(os.Chmod(filepath.Join(workingDir, "Pipfile"), 0000)).To(Succeed()) |
| 60 | + }) |
| 61 | + |
| 62 | + it.After(func() { |
| 63 | + Expect(os.Remove(filepath.Join(workingDir, "Pipfile"))).To(Succeed()) |
| 64 | + }) |
| 65 | + |
| 66 | + it("returns an error", func() { |
| 67 | + _, err := parser.ParseVersion(workingDir) |
| 68 | + Expect(err).To(MatchError(ContainSubstring("permission denied"))) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + context("when the contents of the Pipfile file are malformed", func() { |
| 73 | + it.Before(func() { |
| 74 | + Expect(ioutil.WriteFile( |
| 75 | + filepath.Join(workingDir, "Pipfile"), |
| 76 | + []byte(`%%%%%%%%`), os.ModePerm)).To(Succeed()) |
| 77 | + }) |
| 78 | + |
| 79 | + it.After(func() { |
| 80 | + Expect(os.Remove(filepath.Join(workingDir, "Pipfile"))).To(Succeed()) |
| 81 | + }) |
| 82 | + |
| 83 | + it("returns an error", func() { |
| 84 | + _, err := parser.ParseVersion(workingDir) |
| 85 | + Expect(err).To(MatchError(ContainSubstring("parsing error"))) |
| 86 | + }) |
| 87 | + }) |
| 88 | + }) |
| 89 | + }) |
| 90 | +} |
0 commit comments