GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

args_test.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "testing"
  7. )
  8. func helperCommand(command string, s ...string) (cmd *exec.Cmd) {
  9. cs := []string{"-test.run=TestHelperProcess", "--", command}
  10. cs = append(cs, s...)
  11. cmd = exec.Command(os.Args[0], cs...)
  12. cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
  13. return cmd
  14. }
  15. func TestHelperProcess(t *testing.T) {
  16. if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
  17. return
  18. }
  19. defer os.Exit(0)
  20. args := os.Args
  21. for len(args) > 0 {
  22. if args[0] == "--" {
  23. args = args[1:]
  24. break
  25. }
  26. args = args[1:]
  27. }
  28. if len(args) == 0 {
  29. t.Fatalf("No command\n")
  30. }
  31. _, args = args[0], args[1:]
  32. for _, a := range args {
  33. if a == "user.name" {
  34. fmt.Fprintf(os.Stdout, "Jane Doe")
  35. } else if a == "user.email" {
  36. fmt.Fprintf(os.Stdout, "jd@j.d")
  37. }
  38. }
  39. }
  40. func checkAuthor(t *testing.T, input string, expected string) {
  41. execCommand = helperCommand
  42. defer func() { execCommand = exec.Command }()
  43. author := getUserGitConfigValue(input)
  44. if author != expected {
  45. t.Fatalf("author %q does not match expected value %q", author, expected)
  46. }
  47. }
  48. func TestGetCommitAuthor_OnlyName(t *testing.T) {
  49. checkAuthor(t, "user.name", "Jane Doe")
  50. }
  51. func TestGetCommitAuthor_OnlyEmail(t *testing.T) {
  52. checkAuthor(t, "user.email", "jd@j.d")
  53. }