Hi skillian, very glad to hear from you!
I believe i got your point. My approach wasn’t the best one.
As i have pattern
as a const, it does not allow me to test for inconsistent patterns input.Changing the approach and using regexp.MustCompile
i can have a consistent and testable scenario.
Bottom line, makes sense that, if i don’t have a clear way to handle cases like that (without per example changing the source code to match the errors cases) probably a cleaner way is possible.
If i can, I would like to discuss also one other scenario ( both examples to me boils down to the same aspect ):
Processing a line:
func processLine(line string) int {
count := 0
matched := pattern.MatchString(line)
if matched {
log.Printf("Matched: %s", line)
// Strip eventuals " chars
if err := setEnv(strings.ReplaceAll(line, "\"", "")); err != nil {
log.Fatalf("Error processing line %s: %s", line, err)
}
count++
}
if !matched {
log.Printf("Not matched: %s", line)
}
return count
}
Processing a file:
func (e *env) processFile() {
scanner := bufio.NewScanner(e.fd)
for scanner.Scan() {
line := scanner.Text()
count := processLine(line)
e.loaded += count
}
if err := scanner.Err(); err != nil {
log.Printf("Error line processing: %s\n", err)
}
}
Is there anywhere at the documentationi could consult input data that gives me error at scanner.Errr
and SetEnv
(i’m almost sure this information shouldn’t be at the documentation, but not found it easy way to have the “error” data).
So far i understand that’s the only way to test the error handling cases for scenarios like that:
Or there are ways to stub behavior for functions and force an error?
Thanks in advance for the great discussion.
Regards,
Rafael