Skip to content

Commit

Permalink
new temp dir flag to be able to store temporary files inside docker/k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomassa committed May 24, 2022
1 parent 28073aa commit caeaf7a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
24 changes: 12 additions & 12 deletions acceptanceTests/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ func (d *DiffSuite) TestSimpleDiffWithFullFilePath() {
d.jsonFile1 = file.NewJsonFile(FILE1)
d.jsonFile2 = file.NewJsonFile(FILE2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1053,7 +1053,7 @@ func (d *DiffSuite) TestSimpleDiff() {
d.jsonFile1 = file.NewJsonFile(FILE1)
d.jsonFile2 = file.NewJsonFile(FILE2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1071,7 +1071,7 @@ func (d *DiffSuite) TestSimpleLooseDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_LOOSE1)
d.jsonFile2 = file.NewJsonFile(FILE_LOOSE2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1090,7 +1090,7 @@ func (d *DiffSuite) TestSimpleLooseDiffWithFullFilePath() {
d.jsonFile1 = file.NewJsonFile(FILE_LOOSE1)
d.jsonFile2 = file.NewJsonFile(FILE_LOOSE2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1109,7 +1109,7 @@ func (d *DiffSuite) TestSimpleLooseDiffWithExcludeDescriptions() {
d.jsonFile1 = file.NewJsonFile(FILE_LOOSE1)
d.jsonFile2 = file.NewJsonFile(FILE_LOOSE2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1128,7 +1128,7 @@ func (d *DiffSuite) TestHeadersDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_HEADERS)
d.jsonFile2 = file.NewJsonFile(FILE_HEADERS2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1146,7 +1146,7 @@ func (d *DiffSuite) TestHeadersLooseDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_HEADERS)
d.jsonFile2 = file.NewJsonFile(FILE_HEADERS2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1165,7 +1165,7 @@ func (d *DiffSuite) TestResponsesDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_RESPONSES)
d.jsonFile2 = file.NewJsonFile(FILE_RESPONSES2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1183,7 +1183,7 @@ func (d *DiffSuite) TestResponsesLooseDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_RESPONSES)
d.jsonFile2 = file.NewJsonFile(FILE_RESPONSES2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1202,7 +1202,7 @@ func (d *DiffSuite) TestOperationsDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_OPERATIONS)
d.jsonFile2 = file.NewJsonFile(FILE_OPERATIONS2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1220,7 +1220,7 @@ func (d *DiffSuite) TestOperationsLooseDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_OPERATIONS)
d.jsonFile2 = file.NewJsonFile(FILE_OPERATIONS2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand All @@ -1239,7 +1239,7 @@ func (d *DiffSuite) TestReferencesDiff() {
d.jsonFile1 = file.NewJsonFile(FILE_REFERENCES)
d.jsonFile2 = file.NewJsonFile(FILE_REFERENCES2)

d.vall = validator.NewValidator()
d.vall = validator.NewValidator("")
err := d.vall.InitSchemaFromFile(d.OAS31schemaFile)
if err != nil {
d.T().Error(err)
Expand Down
4 changes: 3 additions & 1 deletion cli/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func RegisterDiffCmd() *cli.Command {
Usage: "Diff between two OAS 3.1 files",
Action: diffCmd,
Flags: []cli.Flag{
TempDirFlag,
BaseFileFlag,
SecondFileFlag,
TypeFilterFlag,
Expand All @@ -37,6 +38,7 @@ func RegisterDiffCmd() *cli.Command {
}

func diffCmd(c *cli.Context) error {
tempDir := c.String(TempDirFlag.Name)
baseFilePath := c.String(BaseFileFlag.Name)
secondFilePath := c.String(SecondFileFlag.Name)
isHtmlOutput := c.Bool(HtmlOutputFlag.Name)
Expand All @@ -54,7 +56,7 @@ func diffCmd(c *cli.Context) error {
return err
}

val := validator.NewValidator()
val := validator.NewValidator(tempDir)
diff := differentiator.NewDifferentiator(val, differentiator.DifferentiatorOptions{
TypeFilter: strings.ToLower(c.String(TypeFilterFlag.Name)),
Loose: c.Bool(LooseFlag.Name),
Expand Down
6 changes: 6 additions & 0 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ var (
Usage: "summary of endpoints based changelog output",
Required: false,
}
TempDirFlag = &cli.StringFlag{
Name: "temp-dir",
Usage: "path for the temporary dir to be used to temporary store oas31 schema files",
Required: false,
Value: "",
}
)
8 changes: 6 additions & 2 deletions cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ func RegisterValidateCmd() *cli.Command {
Aliases: []string{"v"},
Usage: "Validate file to OAS 3.1 schema",
Action: validateCmd,
Flags: []cli.Flag{BaseFileFlag},
Flags: []cli.Flag{
TempDirFlag,
BaseFileFlag,
},
}
}

func validateCmd(c *cli.Context) error {
tempDir := c.String(TempDirFlag.Name)
baseFilePath := c.String(BaseFileFlag.Name)

jsonFile := file.NewJsonFile(baseFilePath)
Expand All @@ -30,7 +34,7 @@ func validateCmd(c *cli.Context) error {
return err
}

val := validator.NewValidator()
val := validator.NewValidator(tempDir)
err = val.InitSchemaFromFile(nil)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion validator/oas31.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (v *validator) InitSchemaFromFile(schemaFile file.JsonFile) error {
}

// we have to create a temp file because the lib only accepts a file or a url
tempFile, err := ioutil.TempFile("", fmt.Sprintf("*.%s", OAS31_SCHEMA_FILE))
tempFile, err := ioutil.TempFile(v.tempDir, fmt.Sprintf("*.%s", OAS31_SCHEMA_FILE))
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ type Validator interface {
}

type validator struct {
tempDir string
compiler *jsonschema.Compiler
schema *jsonschema.Schema
jsonSchema file.JsonFile
}

func NewValidator() Validator {
// passing an empty tempDir will use a temporary dir managed by OS, but it won't work inside docker/k8s
func NewValidator(tempDir string) Validator {
v := &validator{
tempDir: tempDir,
compiler: jsonschema.NewCompiler(),
schema: nil,
jsonSchema: nil,
Expand Down

0 comments on commit caeaf7a

Please sign in to comment.