Skip to content

Commit

Permalink
Add test workflow + unit tests (#54)
Browse files Browse the repository at this point in the history
* Add test workflow

* Add .mockery.yaml file, generate all interface mocks

* Add file module unit tests

* Add config module unit tests

* Use t.TempDir() instead of os.TempDir() for tests

* Cancel in progress workflows

* Remove test

* Fix component controller test

* Fix testutils
  • Loading branch information
matiasdaloia authored Oct 11, 2024
1 parent a3e9ff0 commit 066fd34
Show file tree
Hide file tree
Showing 42 changed files with 3,096 additions and 749 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Test

on:
workflow_dispatch:
pull_request:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
GOLANG_VERSION: "1.21"
NODE_VERSION: "16.x"

jobs:
test-go:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Install Wails
run: go install github.com/wailsapp/wails/v2/cmd/wails@latest
shell: bash

- name: Install Linux Wails deps
run: sudo apt-get update && sudo apt-get install libgtk-3-0 libwebkit2gtk-4.0-dev gcc-aarch64-linux-gnu
shell: bash

- name: Build
run: |
mkdir build
cp -r assets build/assets
wails build
shell: bash

- name: Run tests
uses: robherley/go-test-action@v0
9 changes: 9 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dir: "{{.InterfaceDir}}/mocks"
mockname: "Mock{{.InterfaceName}}"
outpkg: "mocks"
with-expecter: True
all: True
packages:
github.com/scanoss/scanoss.lui:
config:
recursive: True
67 changes: 12 additions & 55 deletions backend/main/internal/testutils.go
Original file line number Diff line number Diff line change
@@ -1,76 +1,33 @@
package internal_test

import (
"os"
"testing"

"github.com/scanoss/scanoss.lui/backend/main/pkg/common/config"
"github.com/scanoss/scanoss.lui/backend/main/pkg/common/scanoss_settings/repository"
"github.com/stretchr/testify/mock"

configEntities "github.com/scanoss/scanoss.lui/backend/main/pkg/common/config/entities"
"github.com/scanoss/scanoss.lui/backend/main/pkg/common/scanoss_settings/entities"
"github.com/scanoss/scanoss.lui/backend/main/pkg/utils"
)

func SetupTestFiles(t *testing.T) string {
t.Helper()

scanSettingsFile, err := os.CreateTemp("", "scanoss-*.json")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
scanSettingsFilePath := scanSettingsFile.Name()

initialScanSettings := entities.SettingsFile{
Bom: entities.Bom{
Include: []entities.ComponentFilter{},
Remove: []entities.ComponentFilter{},
},
}
scanSettingsFile.Close()
if err := utils.WriteJsonFile(scanSettingsFilePath, initialScanSettings); err != nil {
t.Fatalf("Failed to write initial scan settings file: %v", err)
}

return scanSettingsFilePath
}

func InitializeConfig(scanSettingsFilePath string) *configEntities.Config {
config := &configEntities.Config{
ScanSettingsFilePath: scanSettingsFilePath,
}

return config
}

func InitializeTestEnvironment(t *testing.T) func() {
t.Helper()

scanSettingsFilePath := SetupTestFiles(t)

InitializeConfig(scanSettingsFilePath)
cfgPath := t.TempDir() + "/config.json"
configModule := config.NewConfigModule(cfgPath)

cleanup := func() {
os.Remove(scanSettingsFilePath)
err := configModule.Init()
if err != nil {
t.Fatalf("Error initializing config: %s", err.Error())
}

cfg := config.NewConfigModule(scanSettingsFilePath)
cfg.Init()
cfg.LoadConfig()

fr := utils.NewDefaultFileReader()
r := repository.NewScanossSettingsJsonRepository(fr)
settingsFile, err := r.Read()
err = configModule.LoadConfig()
if err != nil {
t.Fatal(err.Error())
}
// Init Scanoss bom module. Set current bom file to singleton
entities.ScanossSettingsJson = &entities.ScanossSettings{
SettingsFile: &settingsFile,
t.Fatalf("Error loading config: %s", err.Error())
}

return cleanup
configModule.Config.ScanRoot = t.TempDir()

return func() {
configModule = nil
}
}

type MockUtils struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package repository

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

"github.com/scanoss/scanoss.lui/backend/main/pkg/common/config/entities"
"github.com/scanoss/scanoss.lui/backend/main/pkg/utils"
"github.com/stretchr/testify/assert"
)

func TestNewConfigJsonRepository(t *testing.T) {
path := "config.json"
repo := NewConfigJsonRepository(path)
assert.Equal(t, path, repo.configPath)
}

func TestConfigJsonRepository_Init(t *testing.T) {
path := "config.json"
repo := NewConfigJsonRepository(path)

err := repo.Init()
assert.NoError(t, err)

fileExists := utils.FileExist(path)
assert.NoError(t, fileExists)

os.Remove(path)
}

func TestConfigJsonRepository_Save(t *testing.T) {
path := t.TempDir() + "config.json"
repo := NewConfigJsonRepository(path)

err := repo.Init()
assert.NoError(t, err)

config := &entities.Config{
ApiUrl: "https://api.scanoss.org",
ApiToken: "test-token",
}

repo.Save(config)

fileData, err := os.ReadFile(path)
assert.NoError(t, err)

savedData, err := utils.JSONParse[map[string]string](fileData)
assert.NoError(t, err)

assert.NoError(t, err)
assert.Equal(t, config.ApiUrl, savedData["apiUrl"])
assert.Equal(t, config.ApiToken, savedData["apiToken"])

os.Remove(path)
}

func TestConfigJsonRepository_Read(t *testing.T) {
path := t.TempDir() + "config.json"
repo := NewConfigJsonRepository(path)
config := &entities.Config{
ApiUrl: "https://api.scanoss.org",
ApiToken: "test-token",
}

err := repo.Init()
assert.NoError(t, err)

repo.Save(config)

readConfig, err := repo.Read()

assert.NoError(t, err)
assert.Equal(t, config.ApiUrl, readConfig.ApiUrl)
assert.Equal(t, config.ApiToken, readConfig.ApiToken)

os.Remove(path)
}

func TestConfigJsonRepository_createConfigFile(t *testing.T) {
path := "/tmp/test_config.json"
repo := NewConfigJsonRepository(path)

err := repo.createConfigFile()
assert.NoError(t, err)

fileExists := utils.FileExist(path)
assert.NoError(t, fileExists)

os.Remove(path)
}

func TestGetDefaultConfigFile(t *testing.T) {
defaultConfig := getDefaultConfigFile()

workingDir, _ := os.Getwd()
expectedResultFilePath := filepath.Join(workingDir, ".scanoss", "results.json")
expectedScanSettingsFilePath := filepath.Join(workingDir, "scanoss.json")

assert.Equal(t, "", defaultConfig.ScanRoot)
assert.Equal(t, expectedResultFilePath, defaultConfig.ResultFilePath)
assert.Equal(t, "", defaultConfig.ApiToken)
assert.Equal(t, "https://api.osskb.org", defaultConfig.ApiUrl)
assert.Equal(t, expectedScanSettingsFilePath, defaultConfig.ScanSettingsFilePath)
}
Loading

0 comments on commit 066fd34

Please sign in to comment.