|
| 1 | +// Copyright New Relic Corporation. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package fs |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestReadFirstLine(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + content string |
| 20 | + expected string |
| 21 | + expectError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "single line", |
| 25 | + content: "Hello World", |
| 26 | + expected: "Hello World", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "multiple lines returns first", |
| 30 | + content: "First Line\nSecond Line\nThird Line", |
| 31 | + expected: "First Line", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "trims whitespace", |
| 35 | + content: " Trimmed Content \nSecond Line", |
| 36 | + expected: "Trimmed Content", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "empty file", |
| 40 | + content: "", |
| 41 | + expected: "", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "only newline", |
| 45 | + content: "\n", |
| 46 | + expected: "", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "tabs and spaces", |
| 50 | + content: "\t Hello \t\nWorld", |
| 51 | + expected: "Hello", |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + tmpFile := createTempFile(t, tt.content) |
| 58 | + defer os.Remove(tmpFile) |
| 59 | + |
| 60 | + result, err := ReadFirstLine(tmpFile) |
| 61 | + if tt.expectError { |
| 62 | + assert.Error(t, err) |
| 63 | + } else { |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.Equal(t, tt.expected, result) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestReadFirstLine_FileNotFound(t *testing.T) { |
| 72 | + result, err := ReadFirstLine("/nonexistent/path/file.txt") |
| 73 | + assert.Error(t, err) |
| 74 | + assert.Equal(t, "unknown", result) |
| 75 | + assert.Contains(t, err.Error(), "cannot open file") |
| 76 | +} |
| 77 | + |
| 78 | +func TestReadFileFieldMatching(t *testing.T) { |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + content string |
| 82 | + pattern string |
| 83 | + expected string |
| 84 | + expectError bool |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "match on first line", |
| 88 | + content: "VERSION=1.2.3\nNAME=test", |
| 89 | + pattern: `VERSION=(.+)`, |
| 90 | + expected: "1.2.3", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "match on second line", |
| 94 | + content: "NAME=test\nVERSION=1.2.3", |
| 95 | + pattern: `VERSION=(.+)`, |
| 96 | + expected: "1.2.3", |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "match with quotes", |
| 100 | + content: `NAME="Ubuntu"\nVERSION="20.04"`, |
| 101 | + pattern: `NAME="([^"]+)"`, |
| 102 | + expected: "Ubuntu", |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "no match returns unknown", |
| 106 | + content: "FOO=bar\nBAZ=qux", |
| 107 | + pattern: `VERSION=(.+)`, |
| 108 | + expected: "unknown", |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "complex regex", |
| 112 | + content: "Red Hat Enterprise Linux release 8.4 (Ootpa)", |
| 113 | + pattern: `release (\d+\.\d+)`, |
| 114 | + expected: "8.4", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "empty file returns unknown", |
| 118 | + content: "", |
| 119 | + pattern: `(.+)`, |
| 120 | + expected: "unknown", |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + tmpFile := createTempFile(t, tt.content) |
| 127 | + defer os.Remove(tmpFile) |
| 128 | + |
| 129 | + re := regexp.MustCompile(tt.pattern) |
| 130 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 131 | + if tt.expectError { |
| 132 | + assert.Error(t, err) |
| 133 | + } else { |
| 134 | + // Note: err may still be nil even when result is "unknown" |
| 135 | + assert.Equal(t, tt.expected, result) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestReadFileFieldMatching_FileNotFound(t *testing.T) { |
| 142 | + re := regexp.MustCompile(`(.+)`) |
| 143 | + result, err := ReadFileFieldMatching("/nonexistent/path/file.txt", re) |
| 144 | + assert.Error(t, err) |
| 145 | + assert.Equal(t, "unknown", result) |
| 146 | + assert.Contains(t, err.Error(), "cannot open file") |
| 147 | +} |
| 148 | + |
| 149 | +func TestReadFileFieldMatching_MultipleCaptures(t *testing.T) { |
| 150 | + content := "VERSION_ID=8.4" |
| 151 | + tmpFile := createTempFile(t, content) |
| 152 | + defer os.Remove(tmpFile) |
| 153 | + |
| 154 | + // Pattern with multiple capture groups - should return first capture |
| 155 | + re := regexp.MustCompile(`VERSION_ID=(\d+)\.(\d+)`) |
| 156 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 157 | + assert.NoError(t, err) |
| 158 | + assert.Equal(t, "8", result) // First capture group |
| 159 | +} |
| 160 | + |
| 161 | +func TestReadFileFieldMatching_LinuxReleaseFiles(t *testing.T) { |
| 162 | + tests := []struct { |
| 163 | + name string |
| 164 | + content string |
| 165 | + pattern string |
| 166 | + expected string |
| 167 | + }{ |
| 168 | + { |
| 169 | + name: "os-release NAME", |
| 170 | + content: `NAME="Ubuntu"\nVERSION="20.04.3 LTS (Focal Fossa)"`, |
| 171 | + pattern: `NAME="?([^"\n]+)"?`, |
| 172 | + expected: "Ubuntu", |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "centos-release", |
| 176 | + content: "CentOS Linux release 7.9.2009 (Core)", |
| 177 | + pattern: `release (\d+\.\d+)`, |
| 178 | + expected: "7.9", |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "redhat-release", |
| 182 | + content: "Red Hat Enterprise Linux release 8.4 (Ootpa)", |
| 183 | + pattern: `release (\d+\.\d+)`, |
| 184 | + expected: "8.4", |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + for _, tt := range tests { |
| 189 | + t.Run(tt.name, func(t *testing.T) { |
| 190 | + tmpFile := createTempFile(t, tt.content) |
| 191 | + defer os.Remove(tmpFile) |
| 192 | + |
| 193 | + re := regexp.MustCompile(tt.pattern) |
| 194 | + result, err := ReadFileFieldMatching(tmpFile, re) |
| 195 | + assert.NoError(t, err) |
| 196 | + assert.Equal(t, tt.expected, result) |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// createTempFile creates a temporary file with the given content and returns its path |
| 202 | +func createTempFile(t *testing.T, content string) string { |
| 203 | + t.Helper() |
| 204 | + tmpDir := t.TempDir() |
| 205 | + tmpFile := filepath.Join(tmpDir, "testfile") |
| 206 | + err := os.WriteFile(tmpFile, []byte(content), 0644) |
| 207 | + require.NoError(t, err) |
| 208 | + return tmpFile |
| 209 | +} |
0 commit comments