|
| 1 | +package bundleinstall_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + bundleinstall "github.com/paketo-buildpacks/bundle-install" |
| 10 | + "github.com/sclevine/spec" |
| 11 | + |
| 12 | + . "github.com/onsi/gomega" |
| 13 | +) |
| 14 | + |
| 15 | +func testRubyVersionFileParser(t *testing.T, context spec.G, it spec.S) { |
| 16 | + var ( |
| 17 | + Expect = NewWithT(t).Expect |
| 18 | + |
| 19 | + path string |
| 20 | + parser bundleinstall.RubyVersionFileParser |
| 21 | + ) |
| 22 | + |
| 23 | + it.Before(func() { |
| 24 | + path = filepath.Join(t.TempDir(), ".ruby-version") |
| 25 | + _, err := os.Create(path) |
| 26 | + Expect(err).NotTo(HaveOccurred()) |
| 27 | + parser = bundleinstall.NewRubyVersionFileParser() |
| 28 | + }) |
| 29 | + |
| 30 | + it.After(func() { |
| 31 | + Expect(os.RemoveAll(path)).To(Succeed()) |
| 32 | + }) |
| 33 | + |
| 34 | + context("ParseVersion", func() { |
| 35 | + context("when the version is just a single Major value", func() { |
| 36 | + it.Before(func() { |
| 37 | + Expect(os.WriteFile(path, []byte("2\n"), 0644)).To(Succeed()) |
| 38 | + }) |
| 39 | + |
| 40 | + it("parses correctly", func() { |
| 41 | + version, err := parser.ParseVersion(path) |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + Expect(version).To(Equal("2")) |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + context("when the version is a Major.Minor value", func() { |
| 48 | + it.Before(func() { |
| 49 | + Expect(os.WriteFile(path, []byte("2.6\n"), 0644)).To(Succeed()) |
| 50 | + }) |
| 51 | + |
| 52 | + it("parses correctly", func() { |
| 53 | + version, err := parser.ParseVersion(path) |
| 54 | + Expect(err).NotTo(HaveOccurred()) |
| 55 | + Expect(version).To(Equal("2.6")) |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + context("when the version is a Major.Minor.Patch value", func() { |
| 60 | + it.Before(func() { |
| 61 | + Expect(os.WriteFile(path, []byte("2.6.3\n"), 0644)).To(Succeed()) |
| 62 | + }) |
| 63 | + |
| 64 | + it("parses correctly", func() { |
| 65 | + version, err := parser.ParseVersion(path) |
| 66 | + Expect(err).NotTo(HaveOccurred()) |
| 67 | + Expect(version).To(Equal("2.6.3")) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + context("when the .ruby-version file does not exist", func() { |
| 72 | + it.Before(func() { |
| 73 | + Expect(os.Remove(path)).To(Succeed()) |
| 74 | + }) |
| 75 | + |
| 76 | + it("returns an ErrNotExist error", func() { |
| 77 | + _, err := parser.ParseVersion(path) |
| 78 | + Expect(errors.Is(err, os.ErrNotExist)).To(BeTrue()) |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + context("failure cases", func() { |
| 83 | + context("when the .ruby-version file cannot be opened", func() { |
| 84 | + it.Before(func() { |
| 85 | + Expect(os.Chmod(path, 0000)).To(Succeed()) |
| 86 | + }) |
| 87 | + |
| 88 | + it("returns an error", func() { |
| 89 | + _, err := parser.ParseVersion(path) |
| 90 | + Expect(err).To(MatchError(ContainSubstring("failed to read .ruby-version file:"))) |
| 91 | + Expect(err).To(MatchError(ContainSubstring("permission denied"))) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + context("when the .ruby-version file contains an invalid version", func() { |
| 96 | + it.Before(func() { |
| 97 | + Expect(os.WriteFile(path, []byte("invalid.version\n"), 0644)).To(Succeed()) |
| 98 | + }) |
| 99 | + |
| 100 | + it("returns an error", func() { |
| 101 | + _, err := parser.ParseVersion(path) |
| 102 | + Expect(err).To(MatchError(ContainSubstring("no valid ruby version found in .ruby-version file:"))) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + }) |
| 107 | + }) |
| 108 | +} |
0 commit comments