From 5f450e54ea611740530fdf5b6424231b1571b415 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 1 Jan 2024 20:53:39 +0100 Subject: [PATCH] add commandline main() test units --- main_test.go | 85 +++++++++++++++++++++++++++++++++++++++++++++ t/config-empty.conf | 2 ++ 2 files changed, 87 insertions(+) create mode 100644 main_test.go create mode 100644 t/config-empty.conf diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..a66a9f0 --- /dev/null +++ b/main_test.go @@ -0,0 +1,85 @@ +/* +Copyright © 2023 Thomas von Dein + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package main + +import ( + "bytes" + "os" + "strings" + "testing" +) + +type Cmdline struct { + name string + args string + expect string + exitcode int +} + +func TestMain(t *testing.T) { + base := "kleingebaeck -c t/config-empty.conf" + cmdlines := []Cmdline{ + { + name: "version", + args: base + " -V", + expect: "This is", + exitcode: 0, + }, + { + name: "help", + args: base + " -h", + expect: "Usage:", + exitcode: 0, + }, + { + name: "debug", + args: base + " -d", + expect: "program_info", + exitcode: 1, + }, + { + name: "no-args-no-user", + args: base, + expect: "invalid or no user id", + exitcode: 1, + }, + // FIXME: add scrape tests as well, re-use scrape_test.go + // stuff or simply move all of it to here + } + + oldargs := os.Args + defer func() { os.Args = oldargs }() + + for _, c := range cmdlines { + var buf bytes.Buffer + os.Args = strings.Split(c.args, " ") + + ret := Main(&buf) + + if ret != c.exitcode { + t.Errorf("%s with cmd <%s> did not exit with %d but %d", + c.name, c.args, c.exitcode, ret) + } + + if !strings.Contains(buf.String(), c.expect) { + t.Errorf("%s with cmd <%s> output did not match.\nExpect: %s\n Got: %s\n", + c.name, c.args, c.expect, buf.String()) + } + } + +} diff --git a/t/config-empty.conf b/t/config-empty.conf new file mode 100644 index 0000000..26f68c3 --- /dev/null +++ b/t/config-empty.conf @@ -0,0 +1,2 @@ +# empty config for Main() unit tests to force unit tests NOT to use an +# eventually existing ~/.kleingebaeck!