Add "echo" as logger

This allow to test ouput, and specify ouput other then os.Stdout
This commit is contained in:
Mikołaj Pęczkowski 2021-11-07 19:58:49 +01:00
parent 23e4547e52
commit 5fd9bc851b
6 changed files with 315 additions and 117 deletions

View file

@ -8,6 +8,7 @@ import (
"gitlab.com/revalus/grm/commands"
"gitlab.com/revalus/grm/config"
"gitlab.com/revalus/grm/echo"
)
type FakeCommandToTest struct {
@ -15,6 +16,21 @@ type FakeCommandToTest struct {
triggerChanged bool
}
type ExpectedMessageTester struct {
expectedMessages []string
}
func (emt ExpectedMessageTester) Write(p []byte) (n int, err error) {
msg := string(p)
if !checkIsItemInSlice(msg, emt.expectedMessages) {
panic(fmt.Sprintf("the message \"%v\"does not match any of the given patterns: %#v", msg, emt.expectedMessages))
} else {
fmt.Println(msg)
}
return 0, nil
}
func (fk FakeCommandToTest) Command(repoCfg config.RepositoryConfig, cmdStatus chan commands.CommandStatus) {
status := commands.CommandStatus{
Name: repoCfg.Name,
@ -100,7 +116,7 @@ func TestParseApplication(t *testing.T) {
}
}
func Example_test_sync_output() {
func TestOutputFromSync(t *testing.T) {
grm := GitRepositoryManager{
configuration: config.Configuration{
Workspace: "/tmp",
@ -108,19 +124,20 @@ func Example_test_sync_output() {
cliArguments: config.CliArguments{
Sync: true,
Version: true,
},
console: ConsoleOutput{
Color: false,
Color: false,
},
}
grm.Run()
// Output:
// Info: Synchronizing repositories
// Info: All repositories are synced
// Info: Current version: 0.3.0
emt := ExpectedMessageTester{
expectedMessages: []string{
"Info: Synchronizing repositories\n",
"Info: All repositories are synced\n",
fmt.Sprintf("Info: Current version: %v\n", VERSION),
},
}
grm.Run(emt)
}
func Example_limit_test_tags() {
func TestLimitTags(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitTags: []string{"example"},
@ -132,22 +149,24 @@ func Example_limit_test_tags() {
{Name: "notExample"},
},
},
console: ConsoleOutput{
Color: false,
},
}
fakeCommand := FakeCommandToTest{
triggerError: false,
triggerChanged: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"example1\": response from fake command\n",
"Repository \"example2\": response from fake command\n",
},
}
echo.Color(false)
echo.Output(emt)
grm.limitTags()
grm.runCommand(fakeCommand)
// Output:
// Repository "example1": response from fake command
// Repository "example2": response from fake command
}
func Example_limit_name() {
func TestLimitName(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitName: "notExample",
@ -159,21 +178,22 @@ func Example_limit_name() {
{Name: "notExample"},
},
},
console: ConsoleOutput{
Color: false,
},
}
fakeCommand := FakeCommandToTest{
triggerError: false,
triggerChanged: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Repository \"notExample\": response from fake command\n",
},
}
echo.Color(false)
echo.Output(emt)
grm.limitName()
grm.runCommand(fakeCommand)
// Output:
// Repository "notExample": response from fake command
}
func Example_run_with_limit_not_existing_name() {
func TestRunWithNotExistingNameInLimit(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitName: "not-existing-name",
@ -185,16 +205,20 @@ func Example_run_with_limit_not_existing_name() {
{Name: "notExample"},
},
},
console: ConsoleOutput{
Color: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Error: no repository was found with the specified name\n",
},
}
grm.Run()
// Output:
// Error: no repository was found with the specified name
echo.Color(false)
status := grm.Run(emt)
if status != 1 {
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
}
}
func Example_run_with_limit_not_existing_tags() {
func TestRunWithNotExistingTagsInLimit(t *testing.T) {
grm := GitRepositoryManager{
cliArguments: config.CliArguments{
LimitTags: []string{"not-existing-tag"},
@ -206,16 +230,20 @@ func Example_run_with_limit_not_existing_tags() {
{Name: "notExample"},
},
},
console: ConsoleOutput{
Color: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Error: no repository was found with the specified tags\n",
},
}
grm.Run()
// Output:
// Error: no repository was found with the specified tags
echo.Color(false)
status := grm.Run(emt)
if status != 1 {
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
}
}
func Example_test_status_output() {
func TestGetStatusOutput(t *testing.T) {
grm := GitRepositoryManager{
configuration: config.Configuration{
Workspace: "/tmp",
@ -223,11 +251,17 @@ func Example_test_status_output() {
cliArguments: config.CliArguments{
Status: true,
},
console: ConsoleOutput{
Color: false,
}
emt := ExpectedMessageTester{
expectedMessages: []string{
"Info: Current status of repositories\n",
},
}
grm.Run()
echo.Color(false)
status := grm.Run(emt)
if status != 0 {
t.Errorf("Expected to get status %v, instead o this got %v", 1, status)
}
// Output:
// Info: Current status of repositories
}