1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/integration/contracts_test.go
Eng Zer Jun 0f4e30e9db
test: use T.TempDir to create temporary test directory (#3721)
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-07-12 15:19:12 +02:00

74 lines
1.9 KiB
Go

package main
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/generator/helper"
"github.com/stretchr/testify/assert"
)
func TestCommandContract(t *testing.T) {
assert.Equal(t, "", "")
}
// Test provided by consumer: SAP InnerSource project
// Changes to the test require peer review by core-team members involved in the project.
func TestGenerator(t *testing.T) {
dir := t.TempDir()
metadata := `metadata:
name: test
description: testDescription
longDescription: testLongDescription
spec:
inputs:
secrets:
- name: secret
description: secretDescription
type: jenkins
params:
- name: testParam
aliases:
- name: testAlias
type: string
description: The name of the Checkmarx project to scan into
mandatory: true
scope:
- PARAMETERS
- STAGES
- STEPS
resourceRef:
- name: commonPipelineEnvironment
param: test/test
outputs:
resources:
- name: influx
type: influx
params:
- name: test_influx
fields:
- name: testfield
- name: commonPipelineEnvironment
type: piperEnvironment
params:
- name: test_cpe
`
ioutil.WriteFile(filepath.Join(dir, "test.yaml"), []byte(metadata), 0755)
openMetaFile := func(name string) (io.ReadCloser, error) { return os.Open(name) }
fileWriter := func(filename string, data []byte, perm os.FileMode) error { return nil }
stepHelperData := helper.StepHelperData{OpenFile: openMetaFile, WriteFile: fileWriter, ExportPrefix: "piperOsCmd"}
metadataFiles, err := helper.MetadataFiles(dir)
assert.NoError(t, err)
err = helper.ProcessMetaFiles(metadataFiles, "./cmd", stepHelperData)
assert.NoError(t, err)
}