package cli import ( "os" "path/filepath" "strings" "testing" "git.avsdev.uk/Go/job-search/internal/config" ) // SkipRulesOptions gives no option without a file, one with a valid file, // and refuses a missing or invalid file, naming the setting. func TestSkipRulesOptions(t *testing.T) { dir := t.TempDir() valid := filepath.Join(dir, "valid.json") invalid := filepath.Join(dir, "invalid.json") os.WriteFile(valid, []byte(`[]`), 0o600) os.WriteFile(invalid, []byte(`[{}]`), 0o600) tests := []struct { file string options int err string }{ {"", 0, ""}, {valid, 1, ""}, {filepath.Join(dir, "missing.json"), 0, "sources.skip_rules_file: "}, {invalid, 0, "sources.skip_rules_file: skip rules "}, } for _, tt := range tests { conf := &config.JobSearchConfig{} conf.Sources.SkipRulesFile = tt.file options, err := SkipRulesOptions(conf) if len(options) != tt.options { t.Errorf("%q: %d options; want %d", tt.file, len(options), tt.options) } if (err == nil) != (tt.err == "") || err != nil && !strings.Contains(err.Error(), tt.err) { t.Errorf("%q: err %v; want %q", tt.file, err, tt.err) } } }