File: env_test.go

package info (click to toggle)
apptainer 1.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,780 kB
  • sloc: sh: 3,329; ansic: 1,706; awk: 414; python: 103; makefile: 54
file content (79 lines) | stat: -rw-r--r-- 2,460 bytes parent folder | download | duplicates (2)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright (c) Contributors to the Apptainer project, established as
//   Apptainer a Series of LF Projects LLC.
//   For website terms of use, trademark policy, privacy policy and other
//   project policies see https://lfprojects.org/policies
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package cmdline

import (
	"testing"

	"github.com/apptainer/apptainer/internal/pkg/test"
	"github.com/spf13/cobra"
)

var cmd cobra.Command

func TestEnvAppendValue(t *testing.T) {
	test.DropPrivilege(t)
	defer test.ResetPrivilege(t)

	cmd.Flags().BoolSlice("boolFlag", []bool{}, "")
	if cmd.Flag("boolFlag").Value.String() != "[]" {
		t.Errorf("The flag should be empty.")
	}

	EnvAppendValue(cmd.Flag("boolFlag"), "1")
	if cmd.Flag("boolFlag").Value.String() != "[true]" {
		t.Errorf("The flag should be set to the value provided.")
	}

	EnvAppendValue(cmd.Flag("boolFlag"), "false")
	if cmd.Flag("boolFlag").Value.String() != "[true,false]" {
		t.Errorf("The flag should be appended with the value provided.")
	}

	EnvAppendValue(cmd.Flag("boolFlag"), "bad")
	if cmd.Flag("boolFlag").Value.String() != "[true,false]" {
		t.Errorf("The flag should return previous value.")
	}
}

func TestEnvSetValue(t *testing.T) {
	test.DropPrivilege(t)
	defer test.ResetPrivilege(t)

	cmd.Flags().Int("intFlag", 0, "")
	if cmd.Flag("intFlag").Value.String() != "0" {
		t.Errorf("The flag should be empty.")
	}

	EnvSetValue(cmd.Flag("intFlag"), "any string")
	if cmd.Flag("intFlag").Value.String() != "0" {
		t.Errorf("The flag should be set to the default value.")
	}

	EnvSetValue(cmd.Flag("intFlag"), "-1")
	if cmd.Flag("intFlag").Value.String() != "-1" {
		t.Errorf("The flag should be set to the value provided.")
	}

	cmd.Flags().StringSlice("stringSlice", []string{""}, "")
	if cmd.Flag("stringSlice").Value.String() != "[]" {
		t.Errorf("The flag should be empty.")
	}

	EnvSetValue(cmd.Flag("stringSlice"), "sliceval1,sliceval2")
	if cmd.Flag("stringSlice").Value.String() != "[sliceval1,sliceval2]" {
		t.Errorf("The flag should be set to value provided.")
	}

	EnvSetValue(cmd.Flag("stringSlice"), "newsliceval")
	if cmd.Flag("stringSlice").Value.String() != "[sliceval1,sliceval2]" {
		t.Errorf("Once set, the flag value should not change.")
	}
}