Skip to content

Commit ff7bb9a

Browse files
Changing functionality of ParseKeyValue and adding tests.
1 parent b8d1f0c commit ff7bb9a

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

harness/variables.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func ParseKeyValue(line, ext string) (string, string) {
172172
return strings.TrimSpace(parts[0]), ""
173173
} else if ext == ".out" {
174174
parts := strings.Fields(line)
175-
if len(parts) == 2 {
176-
return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
175+
if len(parts) > 1 {
176+
return strings.TrimSpace(parts[0]), strings.TrimSpace(strings.Join(parts[1:], " "))
177177
}
178178
return strings.TrimSpace(parts[0]), ""
179179
}

harness/variables_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,45 @@ func TestDeleteOutput(t *testing.T) {
307307
t.Errorf("Expected file to be empty, got: %s", string(content))
308308
}
309309
}
310+
311+
func TestParseKeyValue(t *testing.T) {
312+
tests := []struct {
313+
line string
314+
ext string
315+
expectedKey string
316+
expectedValue string
317+
}{
318+
// .env cases
319+
{"key=value", ".env", "key", "value"},
320+
{"key= value", ".env", "key", "value"},
321+
{"key =value", ".env", "key", "value"},
322+
{"key = value", ".env", "key", "value"},
323+
{"key=", ".env", "key", ""},
324+
{"key", ".env", "key", ""},
325+
{"key=multi word value", ".env", "key", "multi word value"},
326+
{"key= spaced value ", ".env", "key", "spaced value"},
327+
328+
// .out cases
329+
{"key value", ".out", "key", "value"},
330+
{"key value", ".out", "key", "value"},
331+
{" key value ", ".out", "key", "value"},
332+
{"key ", ".out", "key", ""},
333+
{"key", ".out", "key", ""},
334+
{"key multi word value", ".out", "key", "multi word value"},
335+
{"key spaced value ", ".out", "key", "spaced value"},
336+
337+
// Unsupported extension cases
338+
{"key=value", ".unknown", "", ""},
339+
{"key value", ".unknown", "", ""},
340+
}
341+
342+
for _, test := range tests {
343+
t.Run(test.line+"_"+test.ext, func(t *testing.T) {
344+
key, value := ParseKeyValue(test.line, test.ext)
345+
if key != test.expectedKey || value != test.expectedValue {
346+
t.Errorf("For line '%s' and ext '%s': expected ('%s', '%s'), got ('%s', '%s')",
347+
test.line, test.ext, test.expectedKey, test.expectedValue, key, value)
348+
}
349+
})
350+
}
351+
}

0 commit comments

Comments
 (0)