From ffbbc7426b48dd719099eba1a2be46b6a5febb01 Mon Sep 17 00:00:00 2001 From: Mladen Todorovic Date: Tue, 22 Oct 2024 11:19:01 +0200 Subject: [PATCH] Add tests --- cmd/flakechecker/flake_config_test.go | 58 +++++++++++++++++++++ cmd/flakechecker/testdata/flake-config.json | 18 +++++++ 2 files changed, 76 insertions(+) create mode 100644 cmd/flakechecker/flake_config_test.go create mode 100644 cmd/flakechecker/testdata/flake-config.json diff --git a/cmd/flakechecker/flake_config_test.go b/cmd/flakechecker/flake_config_test.go new file mode 100644 index 0000000..076808b --- /dev/null +++ b/cmd/flakechecker/flake_config_test.go @@ -0,0 +1,58 @@ +package main + +import ( + "github.com/stretchr/testify/assert" + "regexp" + "testing" +) + +func TestLoadFlakeConfigFile(t *testing.T) { + samples := []struct { + name string + fileName string + + expectError bool + expectConfig []*flakeCheckerRecord + }{ + { + name: "no config file", + fileName: "no_config.json", + expectError: true, + expectConfig: nil, + }, + { + name: "valid config file", + fileName: "testdata/flake-config.json", + expectError: false, + expectConfig: []*flakeCheckerRecord{ + { + MatchJobName: "pr-.*", + RatioJobName: "main-branch-tests", + TestNameRegex: "TestLoadFlakeConf.*", + Classname: "TestLoadFlakeConfigFile", + RatioThreshold: 5, + regexMatchJobName: regexp.MustCompile("pr-.*"), + regexTestNameRegex: regexp.MustCompile("TestLoadFlakeConf.*"), + }, + { + MatchJobName: "pull-request-tests", + RatioJobName: "main-branch-tests", + TestNameRegex: "TestLoadFlakeConfigFile", + Classname: "TestLoadFlakeConfigFile", + RatioThreshold: 10, + regexMatchJobName: regexp.MustCompile("pull-request-tests"), + regexTestNameRegex: regexp.MustCompile("TestLoadFlakeConfigFile"), + }, + }, + }, + } + + for _, sample := range samples { + t.Run(sample.name, func(tt *testing.T) { + config, err := loadFlakeConfigFile(sample.fileName) + + assert.Equal(tt, sample.expectError, err != nil) + assert.Equal(tt, sample.expectConfig, config) + }) + } +} diff --git a/cmd/flakechecker/testdata/flake-config.json b/cmd/flakechecker/testdata/flake-config.json new file mode 100644 index 0000000..14f814e --- /dev/null +++ b/cmd/flakechecker/testdata/flake-config.json @@ -0,0 +1,18 @@ +[ + { + "_comment": "Config with regex", + "match_job_name": "pr-.*", + "ratio_job_name": "main-branch-tests", + "test_name_regex": "TestLoadFlakeConf.*", + "classname": "TestLoadFlakeConfigFile", + "ratio_threshold": 5 + }, + { + "_comment": "Config with flat values", + "match_job_name": "pull-request-tests", + "ratio_job_name": "main-branch-tests", + "test_name_regex": "TestLoadFlakeConfigFile", + "classname": "TestLoadFlakeConfigFile", + "ratio_threshold": 10 + } +]