Skip to content

Commit

Permalink
Factor out consent test file helper functions into testutils
Browse files Browse the repository at this point in the history
  • Loading branch information
hk21702 committed Jan 21, 2025
1 parent 52d0aa9 commit 5ad0e21
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 66 deletions.
79 changes: 15 additions & 64 deletions internal/consent/consent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ package consent_test

import (
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ubuntu/ubuntu-insights/internal/consent"
"github.com/ubuntu/ubuntu-insights/internal/testutils"
)

// consentDir is a struct that holds a test's temporary directory.
// It should be cleaned up after the test is done.
type consentDir struct {
dir string
}

func TestGetConsentState(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -61,10 +53,10 @@ func TestGetConsentState(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
cDir, err := setupTmpConsentFiles(t, tc.globalFile)
dir, err := setupTmpConsentFiles(t, tc.globalFile)
require.NoError(t, err, "Setup: failed to setup temporary consent files")
defer cDir.cleanup(t)
cm := consent.New(cDir.dir)
defer testutils.CleanupDir(t, dir)
cm := consent.New(dir)

got, err := cm.GetConsentState(tc.source)
if tc.wantErr {
Expand Down Expand Up @@ -118,10 +110,10 @@ func TestSetConsentStates(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
cDir, err := setupTmpConsentFiles(t, tc.globalFile)
dir, err := setupTmpConsentFiles(t, tc.globalFile)
require.NoError(t, err, "Setup: failed to setup temporary consent files")
defer cDir.cleanup(t)
cm := consent.New(cDir.dir)
defer testutils.CleanupDir(t, dir)
cm := consent.New(dir)

err = cm.SetConsentState(tc.writeSource, tc.writeState)
if tc.wantErr {
Expand All @@ -133,7 +125,7 @@ func TestSetConsentStates(t *testing.T) {
states, err := cm.GetAllSourceConsentStates(true)
require.NoError(t, err, "got an unexpected error while getting consent states")

d, err := os.ReadDir(cDir.dir)
d, err := os.ReadDir(dir)
require.NoError(t, err, "failed to read temporary directory")
got := goldenFile{States: states, FileCount: len(d)}

Expand All @@ -143,67 +135,26 @@ func TestSetConsentStates(t *testing.T) {
}
}

// cleanup removes the temporary directory including its contents.
func (cDir consentDir) cleanup(t *testing.T) {
t.Helper()
assert.NoError(t, os.RemoveAll(cDir.dir), "Cleanup: failed to remove temporary directory")
}

func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()

destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()

_, err = io.Copy(destinationFile, sourceFile)
return err
}

func copyDir(srcDir, dstDir string) error {
return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(srcDir, path)
if err != nil {
return err
}
dstPath := filepath.Join(dstDir, relPath)
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}
return copyFile(path, dstPath)
})
}

func setupTmpConsentFiles(t *testing.T, globalFile string) (consentDir, error) {
func setupTmpConsentFiles(t *testing.T, globalFile string) (string, error) {
t.Helper()
cDir := consentDir{}

// Setup temporary directory
var err error
cDir.dir, err = os.MkdirTemp("", "consent-files")
dir, err := os.MkdirTemp("", "consent-files")
if err != nil {
return cDir, fmt.Errorf("failed to create temporary directory: %v", err)
return dir, fmt.Errorf("failed to create temporary directory: %v", err)
}

if err = copyDir(filepath.Join("testdata", "consent_files"), cDir.dir); err != nil {
return cDir, fmt.Errorf("failed to copy testdata directory to temporary directory: %v", err)
if err = testutils.CopyDir(filepath.Join("testdata", "consent_files"), dir); err != nil {
return dir, fmt.Errorf("failed to copy testdata directory to temporary directory: %v", err)
}

// Setup globalFile if provided
if globalFile != "" {
if err = copyFile(filepath.Join(cDir.dir, globalFile), filepath.Join(cDir.dir, "consent.toml")); err != nil {
return cDir, fmt.Errorf("failed to copy requested global consent file: %v", err)
if err = testutils.CopyFile(filepath.Join(dir, globalFile), filepath.Join(dir, "consent.toml")); err != nil {
return dir, fmt.Errorf("failed to copy requested global consent file: %v", err)
}
}

return cDir, nil
return dir, nil
}
4 changes: 2 additions & 2 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const (
// ConsentSourceBaseSeparator is the default separator between the source and the base name of the consent state files.
ConsentSourceBaseSeparator = "-"

// ReportExtension is the default extension for the report files.
ReportExtension = ".json"
// ReportExt is the default extension for the report files.
ReportExt = ".json"
)

type options struct {
Expand Down
54 changes: 54 additions & 0 deletions internal/testutils/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// TiCS: disabled // Test helpers.

package testutils

import (
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

// CleanupDir removes the temporary directory including its contents.
func CleanupDir(t *testing.T, dir string) {
t.Helper()
assert.NoError(t, os.RemoveAll(dir), "Cleanup: failed to remove temporary directory")
}

// CopyFile copies a file from source to destination.
func CopyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()

destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()

_, err = io.Copy(destinationFile, sourceFile)
return err
}

// CopyDir copies the contents of a directory to another directory.
func CopyDir(srcDir, dstDir string) error {
return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(srcDir, path)
if err != nil {
return err
}
dstPath := filepath.Join(dstDir, relPath)
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
}
return CopyFile(path, dstPath)
})
}

0 comments on commit 5ad0e21

Please sign in to comment.