Improve sh config file tweak function to make it stricter with presaving

This commit is contained in:
David Dworken 2023-09-17 14:35:56 -07:00
parent 1d7ba41289
commit 4c912cfaed
No known key found for this signature in database

View File

@ -424,23 +424,23 @@ func isBashProfileConfigured(homedir string) (bool, error) {
} }
func tweakConfigForTests(configContents string) (string, error) { func tweakConfigForTests(configContents string) (string, error) {
madeSubstitution := false substitutionCount := 0
skipLineIndex := -1 removedCount := 0
ret := "" ret := ""
split := strings.Split(configContents, "\n") split := strings.Split(configContents, "\n")
for i, line := range split { for i, line := range split {
if strings.Contains(line, "# Background Run") { if strings.Contains(line, "# Background Run") {
ret += strings.ReplaceAll(split[i+1], "# hishtory", "hishtory") ret += strings.ReplaceAll(split[i+1], "# hishtory", "hishtory")
madeSubstitution = true substitutionCount += 1
skipLineIndex = i + 1 } else if strings.Contains(line, "# Foreground Run") {
} else if i == skipLineIndex { removedCount += 1
continue continue
} else { } else {
ret += line ret += line
} }
ret += "\n" ret += "\n"
} }
if !madeSubstitution { if !(substitutionCount == 2 && removedCount == 2) {
return "", fmt.Errorf("failed to find substitution line in configConents=%#v", configContents) return "", fmt.Errorf("failed to find substitution line in configConents=%#v", configContents)
} }
return ret, nil return ret, nil