-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use exec.d to set PYTHONPYCACHEPREFIX
- Loading branch information
1 parent
36f7178
commit ef29681
Showing
10 changed files
with
155 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package internal_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
) | ||
|
||
func TestUnitEnv(t *testing.T) { | ||
suite := spec.New("cmd/env/internal", spec.Report(report.Terminal{})) | ||
suite("Run", testRun) | ||
suite.Run(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package internal | ||
|
||
import ( | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
// Run will write a TOML environment definition to the output io.Writer with | ||
// the following rules: | ||
// 1. If $PYTHONPYCACHEPREFIX is set, do nothing | ||
// 2. If $PYTHONPYCACHEPREFIX is unset, set it to the expanded value of | ||
// $HOME/.pycache | ||
func Run(env []string, output io.Writer) error { | ||
var home string | ||
for _, v := range env { | ||
if strings.HasPrefix(v, "HOME=") { | ||
home = strings.TrimPrefix(v, "HOME=") | ||
} | ||
|
||
if strings.HasPrefix(v, "PYTHONPYCACHEPREFIX=") { | ||
return nil | ||
} | ||
} | ||
|
||
err := toml.NewEncoder(output).Encode(map[string]string{ | ||
"PYTHONPYCACHEPREFIX": filepath.Join(home, ".pycache"), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package internal_test | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
"github.com/paketo-buildpacks/cpython/cmd/env/internal" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
. "github.com/paketo-buildpacks/packit/v2/matchers" | ||
) | ||
|
||
func testRun(t *testing.T, context spec.G, it spec.S) { | ||
var Expect = NewWithT(t).Expect | ||
|
||
context("when $PYTHONPYCACHEPREFIX is not set", func() { | ||
it("sets it to $HOME/.pycache", func() { | ||
buffer := bytes.NewBuffer(nil) | ||
err := internal.Run([]string{"HOME=/some-home"}, buffer) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(buffer.String()).To(MatchTOML(` | ||
PYTHONPYCACHEPREFIX = "/some-home/.pycache" | ||
`)) | ||
}) | ||
}) | ||
|
||
context("when $PYTHONPYCACHEPREFIX is already set", func() { | ||
it("preserves the existing value", func() { | ||
buffer := bytes.NewBuffer(nil) | ||
err := internal.Run([]string{ | ||
"HOME=/some-home", | ||
"PYTHONPYCACHEPREFIX=/other-home/.pycache", | ||
}, buffer) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(buffer.String()).To(BeEmpty()) | ||
}) | ||
}) | ||
|
||
context("failure cases", func() { | ||
context("when the output cannot be written to", func() { | ||
var file *os.File | ||
|
||
it.Before(func() { | ||
file, err := os.CreateTemp("", "") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(file.Close()).To(Succeed()) | ||
Expect(os.Remove(file.Name())).To(Succeed()) | ||
}) | ||
|
||
it("returns an error", func() { | ||
err := internal.Run([]string{"HOME=/some-home"}, file) | ||
Expect(err).To(MatchError("invalid argument")) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/paketo-buildpacks/cpython/cmd/env/internal" | ||
) | ||
|
||
// env will set environment variables that are dynamically defined at runtime | ||
func main() { | ||
err := internal.Run(os.Environ(), os.NewFile(3, "/dev/fd/3")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters