4
4
package python
5
5
6
6
import (
7
- "bytes"
8
7
"fmt"
9
8
"os"
10
9
"os/exec"
@@ -18,6 +17,7 @@ import (
18
17
type FunctionValidationLayer struct {
19
18
layerContributor libpak.LayerContributor
20
19
logger bard.Logger
20
+ commandRunner CommandRunner
21
21
22
22
module string
23
23
function string
@@ -31,20 +31,22 @@ type FunctionValidationLayer struct {
31
31
32
32
type FunctionValidationOpts func (* FunctionValidationLayer , map [string ]string )
33
33
34
+ //go:generate mockgen -destination ../mock_python/layer.go . Layer
34
35
type Layer interface {
35
36
PythonPath () string
36
37
}
37
38
38
- func NewFunctionValidationLayer (appPath string , invoker Layer , InvokerDepLayer Layer , opts ... FunctionValidationOpts ) FunctionValidationLayer {
39
- fvl := FunctionValidationLayer {
39
+ func NewFunctionValidationLayer (appPath string , invoker Layer , InvokerDepLayer Layer , commandRunner CommandRunner , opts ... FunctionValidationOpts ) * FunctionValidationLayer {
40
+ fvl := & FunctionValidationLayer {
40
41
applicationPath : appPath ,
41
42
Invoker : invoker ,
42
43
InvokerDep : InvokerDepLayer ,
44
+ commandRunner : commandRunner ,
43
45
}
44
46
meta := map [string ]string {}
45
47
46
48
for _ , opt := range opts {
47
- opt (& fvl , meta )
49
+ opt (fvl , meta )
48
50
}
49
51
50
52
fvl .layerContributor = libpak .NewLayerContributor ("validation" , meta , libcnb.LayerTypes {
@@ -54,44 +56,40 @@ func NewFunctionValidationLayer(appPath string, invoker Layer, InvokerDepLayer L
54
56
return fvl
55
57
}
56
58
57
- func (i FunctionValidationLayer ) Contribute (layer libcnb.Layer ) (libcnb.Layer , error ) {
58
- i .layerContributor .Logger = i .logger
59
+ func (f * FunctionValidationLayer ) Contribute (layer libcnb.Layer ) (libcnb.Layer , error ) {
60
+ f .layerContributor .Logger = f .logger
59
61
60
- return i .layerContributor .Contribute (layer , func () (libcnb.Layer , error ) {
61
- i .logger .Body ("Validating function" )
62
+ return f .layerContributor .Contribute (layer , func () (libcnb.Layer , error ) {
63
+ f .logger .Body ("Validating function" )
62
64
63
65
var pythonPath []string
64
- if i .Invoker .PythonPath () != "" {
65
- pythonPath = append (pythonPath , i .Invoker .PythonPath ())
66
+ if f .Invoker .PythonPath () != "" {
67
+ pythonPath = append (pythonPath , f .Invoker .PythonPath ())
66
68
}
67
69
68
- if i .InvokerDep .PythonPath () != "" {
69
- pythonPath = append (pythonPath , i .InvokerDep .PythonPath ())
70
+ if f .InvokerDep .PythonPath () != "" {
71
+ pythonPath = append (pythonPath , f .InvokerDep .PythonPath ())
70
72
}
71
73
72
74
if env , found := os .LookupEnv ("PYTHONPATH" ); found {
73
75
pythonPath = append (pythonPath , env )
74
76
}
75
77
76
- buffer := bytes .NewBuffer (nil )
77
- cmd := exec .Command ("python" , "-m" , "pyfunc" , "check" , "-s" , i .applicationPath )
78
- // cmd := exec.Command("env")
78
+ cmd := exec .Command ("python" , "-m" , "pyfunc" , "check" , "-s" , f .applicationPath )
79
79
cmd .Env = append (os .Environ (), fmt .Sprintf ("PYTHONPATH=%s" , strings .Join (pythonPath , string (os .PathListSeparator ))))
80
- cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , EnvModuleName , i .module ), fmt .Sprintf ("%s=%s" , EnvFunctionName , i .function ))
81
- cmd .Stderr = buffer
82
- cmd .Stdout = buffer
80
+ cmd .Env = append (cmd .Env , fmt .Sprintf ("%s=%s" , EnvModuleName , f .module ), fmt .Sprintf ("%s=%s" , EnvFunctionName , f .function ))
83
81
84
- if err := cmd . Run (); err != nil {
85
- return layer , fmt .Errorf ("%v: %v" , buffer . String () , err )
82
+ if output , err := f . commandRunner . Run (cmd ); err != nil {
83
+ return layer , fmt .Errorf ("%v: %v" , output , err )
86
84
}
87
85
88
- i .logger .Debug ("Function was successfully parsed" )
86
+ f .logger .Debug ("Function was successfully parsed" )
89
87
return layer , nil
90
88
})
91
89
}
92
90
93
- func (i FunctionValidationLayer ) Name () string {
94
- return i .layerContributor .Name
91
+ func (f * FunctionValidationLayer ) Name () string {
92
+ return f .layerContributor .Name
95
93
}
96
94
97
95
func WithValidationLogger (logger bard.Logger ) FunctionValidationOpts {
0 commit comments