mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-02-03 13:21:41 +02:00
0f4e30e9db
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>
124 lines
3.2 KiB
Go
124 lines
3.2 KiB
Go
//go:build integration
|
|
// +build integration
|
|
|
|
// can be execute with go test -tags=integration ./integration/...
|
|
|
|
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
pipergithub "github.com/SAP/jenkins-library/pkg/github"
|
|
"github.com/SAP/jenkins-library/pkg/piperenv"
|
|
)
|
|
|
|
func TestPiperGithubPublishRelease(t *testing.T) {
|
|
t.Parallel()
|
|
token := os.Getenv("PIPER_INTEGRATION_GITHUB_TOKEN")
|
|
if len(token) == 0 {
|
|
t.Fatal("No GitHub token maintained")
|
|
}
|
|
owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
|
|
if len(owner) == 0 {
|
|
owner = "OliverNocon"
|
|
}
|
|
repository := os.Getenv("PIPER_INTEGRATION_GITHUB_REPOSITORY")
|
|
if len(repository) == 0 {
|
|
repository = "piper-integration"
|
|
}
|
|
dir := t.TempDir()
|
|
|
|
testAsset := filepath.Join(dir, "test.txt")
|
|
err := ioutil.WriteFile(testAsset, []byte("Test"), 0644)
|
|
assert.NoError(t, err, "Error when writing temporary file")
|
|
test2Asset := filepath.Join(dir, "test2.txt")
|
|
err = ioutil.WriteFile(test2Asset, []byte("Test"), 0644)
|
|
assert.NoError(t, err, "Error when writing temporary file")
|
|
|
|
t.Run("test single asset - success", func(t *testing.T) {
|
|
//prepare pipeline environment
|
|
now := time.Now()
|
|
piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
|
|
|
|
cmd := command.Command{}
|
|
cmd.SetDir(dir)
|
|
|
|
piperOptions := []string{
|
|
"githubPublishRelease",
|
|
"--owner",
|
|
owner,
|
|
"--repository",
|
|
repository,
|
|
"--token",
|
|
token,
|
|
"--assetPath",
|
|
testAsset,
|
|
"--noTelemetry",
|
|
}
|
|
|
|
err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
|
|
assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
|
|
})
|
|
t.Run("test multiple assets - success", func(t *testing.T) {
|
|
//prepare pipeline environment
|
|
now := time.Now()
|
|
piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
|
|
|
|
cmd := command.Command{}
|
|
cmd.SetDir(dir)
|
|
|
|
piperOptions := []string{
|
|
"githubPublishRelease",
|
|
"--owner",
|
|
owner,
|
|
"--repository",
|
|
repository,
|
|
"--token",
|
|
token,
|
|
"--assetPathList",
|
|
testAsset,
|
|
"--assetPathList",
|
|
test2Asset,
|
|
"--noTelemetry",
|
|
}
|
|
|
|
err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
|
|
assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
|
|
})
|
|
}
|
|
|
|
func TestGithubFetchCommitStatistics(t *testing.T) {
|
|
t.Parallel()
|
|
// prepare
|
|
token := os.Getenv("PIPER_INTEGRATION_GITHUB_TOKEN")
|
|
if len(token) == 0 {
|
|
t.Fatal("No GitHub token maintained")
|
|
}
|
|
|
|
owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
|
|
if len(owner) == 0 {
|
|
owner = "OliverNocon"
|
|
}
|
|
repository := os.Getenv("PIPER_INTEGRATION_GITHUB_REPOSITORY")
|
|
if len(repository) == 0 {
|
|
repository = "piper-integration"
|
|
}
|
|
// test
|
|
result, err := pipergithub.FetchCommitStatistics(&pipergithub.FetchCommitOptions{
|
|
Owner: owner, Repository: repository, APIURL: "https://api.github.com", Token: token, SHA: "3601ed6"})
|
|
|
|
// assert
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 2, result.Additions)
|
|
assert.Equal(t, 0, result.Deletions)
|
|
assert.Equal(t, 2, result.Total)
|
|
assert.Equal(t, 1, result.Files)
|
|
}
|