Add a way to change the working directory

This commit is contained in:
David Vogel 2025-11-03 19:50:24 +01:00
parent 79b86fb22c
commit bd9c1beac7
3 changed files with 26 additions and 2 deletions

View File

@ -3,6 +3,7 @@
"Dadido",
"Foogaloo",
"golangci",
"somepath",
"typst",
"Vogel"
]

5
cli.go
View File

@ -17,7 +17,8 @@ import (
// TODO: Add an interface for the Typst caller and let CLI (and later docker and WASM) be implementations of that
type CLI struct {
ExecutablePath string // The Typst executable path can be overridden here. Otherwise the default path will be used.
ExecutablePath string // The Typst executable path can be overridden here. Otherwise the default path will be used.
WorkingDirectory string // The path where the Typst executable is run in. When left empty, the Typst executable will be run in the process's current directory.
}
// TODO: Add method for querying the Typst version resulting in a semver object
@ -31,6 +32,7 @@ func (c CLI) VersionString() (string, error) {
}
cmd := exec.Command(execPath, "--version")
cmd.Dir = c.WorkingDirectory // This doesn't do anything, but we will do it anyways for consistency.
var output, errBuffer bytes.Buffer
cmd.Stdout = &output
@ -64,6 +66,7 @@ func (c CLI) Compile(input io.Reader, output io.Writer, options *CLIOptions) err
}
cmd := exec.Command(execPath, args...)
cmd.Dir = c.WorkingDirectory
cmd.Stdin = input
cmd.Stdout = output

View File

@ -1,4 +1,4 @@
// Copyright (c) 2024 David Vogel
// Copyright (c) 2024-2025 David Vogel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
@ -9,6 +9,7 @@ import (
"bytes"
"image"
_ "image/png"
"path/filepath"
"strconv"
"testing"
@ -60,3 +61,22 @@ func TestCLI_Compile(t *testing.T) {
t.Fatalf("Resulting image height is %d, expected %d.", imgConf.Height, inches*ppi)
}
}
// Test basic compile functionality with a given working directory.
func TestCLI_CompileWithWorkingDir(t *testing.T) {
cli := typst.CLI{
WorkingDirectory: filepath.Join(".", "test-files"),
}
r := bytes.NewBufferString(`#import "hello-world-template.typ": template
#show: doc => template()`)
var w bytes.Buffer
err := cli.Compile(r, &w, nil)
if err != nil {
t.Fatalf("Failed to compile document: %v.", err)
}
if w.Available() == 0 {
t.Errorf("No output was written.")
}
}