github.com/renbou/grpcbridge@v0.0.2-0.20240416012907-bcbd8b12648a/internal/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"log/slog"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  // chdir from https://github.com/golang/go/issues/45182, while t.Chdir isn't made public
    10  func chdir(t *testing.T, dir string) func() {
    11  	wd, err := os.Getwd()
    12  	if err != nil {
    13  		t.Fatalf("getting current working directory prior to test execution: %s", err)
    14  	}
    15  	if err := os.Chdir(dir); err != nil {
    16  		t.Fatalf("changing directory to %s: %s", dir, err)
    17  	}
    18  
    19  	return func() {
    20  		if err := os.Chdir(wd); err != nil {
    21  			t.Fatalf("restoring working directory to %s: %s", wd, err)
    22  		}
    23  	}
    24  }
    25  
    26  // Test_discoverPath runs without t.Parallel because it would interfere
    27  // with other tests when changing directories.
    28  func Test_discoverPath(t *testing.T) {
    29  	tests := []struct {
    30  		name  string
    31  		env   map[string]string
    32  		files []string
    33  
    34  		discovered string
    35  		origin     discoveryOrigin
    36  		ok         bool
    37  	}{
    38  		{
    39  			name: "env",
    40  			env: map[string]string{
    41  				"GRPCBRIDGE_CONFIG": "config.hcl",
    42  			},
    43  			discovered: "config.hcl",
    44  			origin:     discoveryOriginEnv,
    45  			ok:         true,
    46  		},
    47  		{
    48  			name:       "grpcbridge.hcl",
    49  			files:      []string{"grpcbridge.hcl"},
    50  			discovered: "grpcbridge.hcl",
    51  			origin:     discoveryOriginAuto,
    52  			ok:         true,
    53  		},
    54  		{
    55  			name:       "grpcbridge.json",
    56  			files:      []string{"grpcbridge.json"},
    57  			discovered: "grpcbridge.json",
    58  			origin:     discoveryOriginAuto,
    59  			ok:         true,
    60  		},
    61  		{
    62  			name:       "config.hcl",
    63  			files:      []string{"config.hcl"},
    64  			discovered: "config.hcl",
    65  			origin:     discoveryOriginAuto,
    66  			ok:         true,
    67  		},
    68  		{
    69  			name:       "config.json",
    70  			files:      []string{"config.json"},
    71  			discovered: "config.json",
    72  			origin:     discoveryOriginAuto,
    73  			ok:         true,
    74  		},
    75  		{
    76  			name:       "prefer specific hcl",
    77  			files:      []string{"grpcbridge.hcl", "config.hcl"},
    78  			discovered: "grpcbridge.hcl",
    79  			origin:     discoveryOriginAuto,
    80  			ok:         true,
    81  		},
    82  		{
    83  			name:       "prefer specific json",
    84  			files:      []string{"grpcbridge.json", "config.json"},
    85  			discovered: "grpcbridge.json",
    86  			origin:     discoveryOriginAuto,
    87  			ok:         true,
    88  		},
    89  		{
    90  			name: "prefer env",
    91  			env: map[string]string{
    92  				"GRPCBRIDGE_CONFIG": "config.hcl",
    93  			},
    94  			files:      []string{"grpcbridge.hcl"},
    95  			discovered: "config.hcl",
    96  			origin:     discoveryOriginEnv,
    97  			ok:         true,
    98  		},
    99  		{
   100  			name:   "fail",
   101  			origin: discoveryOriginNone,
   102  			ok:     false,
   103  		},
   104  	}
   105  
   106  	for _, tt := range tests {
   107  		// Subtests also run without t.Parallel for the same reason as the main test
   108  		t.Run(tt.name, func(t *testing.T) {
   109  			defer chdir(t, t.TempDir())()
   110  
   111  			// Prepare environment variables and files for this single test.
   112  			// The files will be automatically cleared thanks to t.TempDir.
   113  			for k, v := range tt.env {
   114  				t.Setenv(k, v)
   115  			}
   116  
   117  			for _, filename := range tt.files {
   118  				f, err := os.Create(filename)
   119  				if err != nil {
   120  					t.Fatalf("creating test file %s: %s", filename, err)
   121  				}
   122  
   123  				_ = f.Close()
   124  			}
   125  
   126  			discovered, origin, ok := discoverPath()
   127  
   128  			if discovered != tt.discovered {
   129  				t.Errorf("DiscoverConfig() returned config file = %q, want %q", discovered, tt.discovered)
   130  			}
   131  
   132  			if origin != tt.origin {
   133  				t.Errorf("DiscoverConfig() returned origin = %s, want %s", origin, tt.origin)
   134  			}
   135  
   136  			if ok != tt.ok {
   137  				t.Errorf("DiscoverConfig() returned ok = %t, want %t", ok, tt.ok)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  // Test_logLevel runs without t.Parallel because it would interfere
   144  // with other tests when changing environment variables.
   145  func Test_logLevel(t *testing.T) {
   146  	tests := []struct {
   147  		value     string
   148  		wantLevel slog.Level
   149  	}{
   150  		{
   151  			value:     "",
   152  			wantLevel: slog.LevelInfo,
   153  		},
   154  		{
   155  			value:     "debug",
   156  			wantLevel: slog.LevelDebug,
   157  		},
   158  		{
   159  			value:     "info",
   160  			wantLevel: slog.LevelInfo,
   161  		},
   162  		{
   163  			value:     "warn",
   164  			wantLevel: slog.LevelWarn,
   165  		},
   166  		{
   167  			value:     "error",
   168  			wantLevel: slog.LevelError,
   169  		},
   170  		{
   171  			value:     "invalid",
   172  			wantLevel: slog.LevelInfo,
   173  		},
   174  	}
   175  
   176  	for _, tt := range tests {
   177  		// Subtests also run without t.Parallel for the same reason as the main test.
   178  		t.Run(tt.value, func(t *testing.T) {
   179  			// Arrange
   180  			t.Setenv(envPrefix+"LOG_LEVEL", tt.value)
   181  
   182  			// Act
   183  			level := LogLevel()
   184  
   185  			// Assert
   186  			if level != tt.wantLevel {
   187  				t.Errorf("LogLevel() = %v, want %v", level, tt.wantLevel)
   188  			}
   189  		})
   190  	}
   191  }