wails/v2/pkg/templates/templates_test.go
Lea Anthony 718fd92f85
fix(v2): prevent wails init in non-empty directory with -d flag (#4955)
* fix(v2): prevent wails init in non-empty directory with -d flag

When using -d to specify a target directory, wails init now checks if
the directory is non-empty and errors if so. This prevents accidental
data loss (e.g., overwriting .git directories).

Fixes #4940

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* test(v2): add tests for init non-empty directory check

Add tests to verify:
- Install fails when target directory is non-empty
- Install succeeds when target directory is empty

Also update changelog with the fix.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* Apply suggestions from code review

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
2026-02-04 21:23:07 +11:00

99 lines
1.9 KiB
Go

package templates
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/matryer/is"
)
func TestList(t *testing.T) {
is2 := is.New(t)
templateList, err := List()
is2.NoErr(err)
is2.Equal(len(templateList), 13)
}
func TestShortname(t *testing.T) {
is2 := is.New(t)
vanillaTemplate, err := getTemplateByShortname("vanilla")
is2.NoErr(err)
is2.Equal(vanillaTemplate.Name, "Vanilla + Vite")
}
func TestInstall(t *testing.T) {
is2 := is.New(t)
// Change to the directory of this file
_, filename, _, _ := runtime.Caller(0)
err := os.Chdir(filepath.Dir(filename))
is2.NoErr(err)
options := &Options{
ProjectName: "test",
TemplateName: "vanilla",
AuthorName: "Lea Anthony",
AuthorEmail: "lea.anthony@gmail.com",
}
defer func() {
_ = os.RemoveAll(options.ProjectName)
}()
_, _, err = Install(options)
is2.NoErr(err)
}
func TestInstallFailsInNonEmptyDirectory(t *testing.T) {
is2 := is.New(t)
// Create a temp directory with a file in it
tempDir, err := os.MkdirTemp("", "wails-test-nonempty-*")
is2.NoErr(err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
// Create a file in the directory to make it non-empty
err = os.WriteFile(filepath.Join(tempDir, "existing-file.txt"), []byte("test"), 0644)
is2.NoErr(err)
options := &Options{
ProjectName: "test",
TemplateName: "vanilla",
TargetDir: tempDir,
}
_, _, err = Install(options)
is2.True(err != nil) // Should fail
is2.True(err.Error() == "cannot initialise project in non-empty directory: "+tempDir)
}
func TestInstallSucceedsInEmptyDirectory(t *testing.T) {
is2 := is.New(t)
// Create an empty temp directory
tempDir, err := os.MkdirTemp("", "wails-test-empty-*")
is2.NoErr(err)
defer func() {
_ = os.RemoveAll(tempDir)
}()
options := &Options{
ProjectName: "test",
TemplateName: "vanilla",
TargetDir: tempDir,
}
_, _, err = Install(options)
is2.NoErr(err) // Should succeed in empty directory
}