Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
feat: Add check for minimum python version
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Jun 17, 2024
1 parent 1e1bd87 commit 5ff4a5f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kluctl/go-jinja2
go 1.19

require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/go-git/go-git/v5 v5.12.0
github.com/gobwas/glob v0.2.3
github.com/hashicorp/go-multierror v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
27 changes: 27 additions & 0 deletions jinja2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jinja2

import (
"fmt"
"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"github.com/gobwas/glob"
"github.com/hashicorp/go-multierror"
Expand All @@ -16,6 +17,8 @@ import (
"sync"
)

var minimumPythonVersion = semver.MustParse("3.10.0")

type Jinja2 struct {
ep python.Python
extractDir string
Expand Down Expand Up @@ -83,6 +86,11 @@ func NewJinja2(name string, parallelism int, opts ...Jinja2Opt) (*Jinja2, error)
}
j2.ep.AddPythonPath(j2.jinja2Lib.GetExtractedPath())

err = j2.checkPythonVersion()
if err != nil {
return nil, err
}

j2.rendererSrc, err = embed_util.NewEmbeddedFilesWithTmpDir(python_src.RendererSource, tmpDir+"-jinja2-renderer", true)
if err != nil {
return nil, err
Expand Down Expand Up @@ -114,6 +122,25 @@ func NewJinja2(name string, parallelism int, opts ...Jinja2Opt) (*Jinja2, error)
return j2, nil
}

func (j *Jinja2) checkPythonVersion() error {
cmd, err := j.ep.PythonCmd("-c", `import platform; print(platform.python_version())`)
if err != nil {
return err
}
v, err := cmd.Output()
if err != nil {
return err
}
v2, err := semver.NewVersion(strings.TrimSpace(string(v)))
if err != nil {
return fmt.Errorf("failed to parse python version: %w", err)
}
if v2.LessThan(minimumPythonVersion) {
return fmt.Errorf("python version (%s) must be at least %s", v2.String(), minimumPythonVersion)
}
return nil
}

func (j *Jinja2) Close() {
for i := 0; i < j.parallelism; i++ {
pj := <-j.pj
Expand Down
18 changes: 18 additions & 0 deletions jinja2_external_python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jinja2

import (
"fmt"
"github.com/Masterminds/semver/v3"
"github.com/kluctl/go-embed-python/python"
"github.com/stretchr/testify/assert"
"math/rand"
Expand Down Expand Up @@ -34,3 +35,20 @@ func TestSystemPython(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "a", r1)
}

func TestSystemPythonVersion(t *testing.T) {
backup := minimumPythonVersion
t.Cleanup(func() {
minimumPythonVersion = backup
})

minimumPythonVersion = semver.MustParse("10.0.0")

ep := python.NewPython()
if _, err := ep.GetExePath(); err != nil {
t.Skipf("skipping due to missing python binary")
}

_, err := newJinja2WithErr(t, WithPython(ep))
assert.ErrorContains(t, err, "must be at least 10.0.0")
}
12 changes: 10 additions & 2 deletions jinja2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ import (
"testing"
)

func newJinja2(t *testing.T, opts ...Jinja2Opt) *Jinja2 {
func newJinja2WithErr(t *testing.T, opts ...Jinja2Opt) (*Jinja2, error) {
name := fmt.Sprintf("jinja2-%d", rand.Uint32())
opts2 := append(opts, WithExtension("go_jinja2.ext.kluctl"))
j2, err := NewJinja2(name, 1, opts2...)
if err != nil {
t.Fatal(err)
return nil, err
}

t.Cleanup(j2.Close)
t.Cleanup(j2.Cleanup)

return j2, nil
}

func newJinja2(t *testing.T, opts ...Jinja2Opt) *Jinja2 {
j2, err := newJinja2WithErr(t, opts...)
if err != nil {
t.Fatal(err)
}
return j2
}

Expand Down

0 comments on commit 5ff4a5f

Please sign in to comment.