~sbinet/star-tex-patches

[PATCH star-tex 3/3] cmd/kpath-find: use kpath.NewFromFS to create contexts

Details
Message ID
<T51PVsDepwOOd0jJdKSzhTweLxWzh7TEZFsRNOuog@cp4-web-031.plabs.ch>
DKIM signature
missing
Download raw message
Patch: +49 -44
Signed-off-by: Sebastien Binet <s@sbinet.org>
---
 cmd/kpath-find/main.go      | 34 +++++++++++----------
 cmd/kpath-find/main_test.go | 59 +++++++++++++++++++------------------
 2 files changed, 49 insertions(+), 44 deletions(-)

diff --git a/cmd/kpath-find/main.go b/cmd/kpath-find/main.go
index 97f836b..ea753a5 100644
--- a/cmd/kpath-find/main.go
+++ b/cmd/kpath-find/main.go
@@ -8,10 +8,11 @@ package main
import (
	"flag"
	"fmt"
	"io/fs"
	"log"
	"os"
	"path/filepath"

	"star-tex.org/x/tex/internal/tds"
	"star-tex.org/x/tex/kpath"
)

@@ -40,17 +41,21 @@ func main() {
				break loop
			}
		}
		if *texmf == "" {
			*texmf = "/usr/share/texmf-dist"
		}
	}

	dbname := filepath.Join(*texmf, "ls-R")
	xmain(flag.Arg(0), dbname, *all)
	dir := *texmf
	xmain(flag.Arg(0), dir, *all)
}

func xmain(name, dbname string, all bool) {
	fnames, err := process(name, dbname, all)
func xmain(name, dbdir string, all bool) {
	var db fs.FS
	switch dbdir {
	case "":
		db = tds.FS
	default:
		db = os.DirFS(dbdir)
	}
	fnames, err := process(name, db, all)
	if err != nil {
		log.Fatal(err)
	}
@@ -59,14 +64,13 @@ func xmain(name, dbname string, all bool) {
	}
}

func process(name, dbname string, all bool) ([]string, error) {
	db, err := os.Open(dbname)
	if err != nil {
		return nil, fmt.Errorf("could not open texmf db %q: %w", dbname, err)
	}
	defer db.Close()
func process(name string, fsys fs.FS, all bool) ([]string, error) {
	var (
		ctx kpath.Context
		err error
	)

	ctx, err := kpath.NewFromDB(db)
	ctx, err = kpath.NewFromFS(fsys)
	if err != nil {
		return nil, fmt.Errorf("could not create kpath context: %w", err)
	}
diff --git a/cmd/kpath-find/main_test.go b/cmd/kpath-find/main_test.go
index 5e1eda5..22f6b1c 100644
--- a/cmd/kpath-find/main_test.go
+++ b/cmd/kpath-find/main_test.go
@@ -6,11 +6,14 @@ package main

import (
	"fmt"
	"io/fs"
	"os"
	stdpath "path"
	"reflect"
	"runtime"
	"testing"

	"star-tex.org/x/tex/internal/tds"
)

func TestProcess(t *testing.T) {
@@ -31,10 +34,11 @@ file2.tex

./dir3:
file2.tex
`), 0755)
`), 0644)
	if err != nil {
		t.Fatalf("could not create texmf db: %+v", err)
	}
	db := os.DirFS(dir)

	errNotThere := "no such file or directory"
	if runtime.GOOS == "windows" {
@@ -42,53 +46,50 @@ file2.tex
	}

	for _, tc := range []struct {
		name   string
		dbname string
		all    bool
		want   []string
		err    error
		name string
		db   fs.FS
		all  bool
		want []string
		err  error
	}{
		{
			name:   "file1.tex",
			dbname: dbname,
			want:   []string{stdpath.Join(dir, "/dir1/file1.tex")},
			name: "file1.tex",
			db:   db,
			want: []string{stdpath.Join(dir, "/dir1/file1.tex")},
		},
		{
			name:   "file2.tex",
			dbname: dbname,
			all:    true,
			name: "file2.tex",
			db:   db,
			all:  true,
			want: []string{
				stdpath.Join(dir, "/dir2/file2.tex"),
				stdpath.Join(dir, "/dir3/file2.tex"),
			},
		},
		{
			name:   "file2.tex",
			dbname: dbname,
			err:    fmt.Errorf(`kpath: too many hits for file "file2.tex" (n=2)`),
			name: "file2.tex",
			db:   db,
			err:  fmt.Errorf(`kpath: too many hits for file "file2.tex" (n=2)`),
		},
		{
			name:   "err-new-from-db",
			dbname: "/dev/full",
			name: "err-no-db-file",
			db:   os.DirFS(stdpath.Join(dir, "not-there")),
			err: fmt.Errorf(
				`could not create kpath context: ` +
					`kpath: could not parse db file: ` +
					`could not scan db file: bufio.Scanner: token too long`,
			),
		},
		{
			name:   "err-no-db-file",
			dbname: stdpath.Join(dir, "not-there"),
			err: fmt.Errorf(
				`could not open texmf db %[1]q: `+
					`open %[1]s: %s`,
				`could not create kpath context: `+
					`kpath: could not walk fs: `+
					`open %[1]s/.: %[2]s`,
				stdpath.Join(dir, "not-there"),
				errNotThere,
			),
		},
		{
			name: "plain.tex",
			db:   tds.FS,
			want: []string{"tex/plain/base/plain.tex"},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			got, err := process(tc.name, tc.dbname, tc.all)
			got, err := process(tc.name, tc.db, tc.all)
			switch {
			case err == nil && tc.err == nil:
				// ok.
-- 
2.30.1
Reply to thread Export thread (mbox)