File: flag.go

package info (click to toggle)
mtail 3.2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,384 kB
  • sloc: yacc: 647; makefile: 226; sh: 78; lisp: 77; awk: 17
file content (27 lines) | stat: -rw-r--r-- 558 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Copyright 2019 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package testutil

import (
	"flag"
	"testing"
)

// SetFlag sets the value of the commandline flag, and registers a cleanup function that restores the flag value.
func SetFlag(tb testing.TB, name, value string) {
	tb.Helper()
	val := flag.Lookup(name)

	if err := flag.Set(name, value); err != nil {
		tb.Fatal(err)
	}

	tb.Cleanup(func() {
		if val != nil {
			if err := flag.Set(name, val.Value.String()); err != nil {
				tb.Fatal(err)
			}
		}
	})
}