diff options
| -rw-r--r-- | internal/cli/cli.go | 34 | ||||
| -rw-r--r-- | internal/cli/cli_test.go | 18 | ||||
| -rw-r--r-- | internal/config/config.go | 2 |
3 files changed, 35 insertions, 19 deletions
diff --git a/internal/cli/cli.go b/internal/cli/cli.go index ff1299a..04defbf 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -46,7 +46,8 @@ Flags: --list list all books + abbreviations (dialect from sigla_style) --citation print the day's gospel reference (scripts/cron) and exit --week list the coming week's gospel references and exit - --rand, --rand-v print a random verse (corpus version) and exit + --rand, --rand-v print a random verse and exit (uses default_version or -b VER; + bt has no corpus, so it falls back to a corpus version) --rand-ch print a random chapter and exit -v, --version print the version and exit -h, --help this help @@ -710,23 +711,26 @@ func renderCompare(cfg config.Config, list, date string, all, raw bool, width in return 0 } -// randVersion picks the corpus version for --rand: an explicit -b VER (rejected -// if it has no corpus), else the configured default_version if it has a corpus, -// else the first corpus version in cfg.Versions, else "wuj". So a config whose -// default is "bt" (no corpus) still yields a random passage. +// randVersion picks the corpus version for --rand: an explicit -b VER (any of +// the five is accepted), else the configured default_version. "bt" is allowed +// but has no embedded corpus (it is the niedziela.pl scrape), so it -- like an +// unset default -- transparently falls back to the first corpus version in +// cfg.Versions (else "wuj"), so --rand always yields a passage. Only an unknown +// version code is an error. func randVersion(cfg config.Config, bibleVer string) (string, error) { - if bibleVer != "" { - if !isCorpusVersion(bibleVer) { - return "", fmt.Errorf("--rand needs a corpus version; %q has no full text (use wuj, vul, grb, drb)", bibleVer) - } - return bibleVer, nil + v := bibleVer + if v == "" { + v = cfg.DefaultVersion } - if isCorpusVersion(cfg.DefaultVersion) { - return cfg.DefaultVersion, nil + if v != "" && !config.ValidVersion(v) { + return "", fmt.Errorf("unknown version %q (want one of bt, wuj, vul, grb, drb)", v) } - for _, v := range cfg.Versions { - if isCorpusVersion(v) { - return v, nil + if isCorpusVersion(v) { + return v, nil + } + for _, c := range cfg.Versions { + if isCorpusVersion(c) { + return c, nil } } return "wuj", nil diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 58b2d00..3b99562 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -375,10 +375,22 @@ func TestRandChapter(t *testing.T) { } } -func TestRandRejectsBT(t *testing.T) { +func TestRandBTFallsBack(t *testing.T) { + // bt has no corpus but is accepted: --rand falls back to a corpus version + // and still prints a passage (exit 0), rather than erroring. var out, errb bytes.Buffer - if code := Run([]string{"--rand-v", "-b", "bt"}, nil, &out, &errb); code != 2 { - t.Errorf("rand -b bt code=%d want 2 (stderr=%q)", code, errb.String()) + if code := Run([]string{"--rand-v", "-b", "bt"}, nil, &out, &errb); code != 0 { + t.Errorf("rand -b bt code=%d want 0 (stderr=%q)", code, errb.String()) + } + if strings.TrimSpace(out.String()) == "" { + t.Errorf("rand -b bt produced no output") + } +} + +func TestRandRejectsUnknownVersion(t *testing.T) { + var out, errb bytes.Buffer + if code := Run([]string{"--rand-v", "-b", "xyz"}, nil, &out, &errb); code != 2 { + t.Errorf("rand -b xyz code=%d want 2 (stderr=%q)", code, errb.String()) } } diff --git a/internal/config/config.go b/internal/config/config.go index 3087d6f..e3299e1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -25,7 +25,7 @@ var seedTOML []byte // Version is lectio's release version, shared by every binary's // -v/--version output (lectio, lectio-ui, lectio-web). -const Version = "0.16.0" +const Version = "0.16.1" // validVersions are the five scripture versions lectio understands. var validVersions = map[string]bool{ |
