Skip to content

Commit

Permalink
add test case for -fail flag
Browse files Browse the repository at this point in the history
  • Loading branch information
benyanke committed Sep 17, 2024
1 parent 417e390 commit b367dcb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
48 changes: 48 additions & 0 deletions cmd/templ/fmtcmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestFormat(t *testing.T) {
Files: []string{
tp.testFiles["a.templ"].name,
},
FailIfChanged: false,
}); err != nil {
t.Fatalf("failed to run format command: %v", err)
}
Expand All @@ -101,6 +102,7 @@ func TestFormat(t *testing.T) {
Files: []string{
tp.testFiles["a.templ"].name,
},
FailIfChanged: false,
}); err != nil {
t.Fatalf("failed to run format command: %v", err)
}
Expand All @@ -112,4 +114,50 @@ func TestFormat(t *testing.T) {
t.Error(diff)
}
})

t.Run("fails when fail flag used and change occurs", func(t *testing.T) {
tp, err := setupProjectDir()
if err != nil {
t.Fatalf("failed to setup project dir: %v", err)
}
defer tp.cleanup()
if err = Run(log, nil, nil, Arguments{
Files: []string{
tp.testFiles["a.templ"].name,
},
FailIfChanged: true,
}); err == nil {
t.Fatal("command should have exited with an error and did not")
}
data, err := os.ReadFile(tp.testFiles["a.templ"].name)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
if diff := cmp.Diff(tp.testFiles["a.templ"].expected, string(data)); diff != "" {
t.Error(diff)
}
})

t.Run("passes when fail flag used and no change occurs", func(t *testing.T) {
tp, err := setupProjectDir()
if err != nil {
t.Fatalf("failed to setup project dir: %v", err)
}
defer tp.cleanup()
if err = Run(log, nil, nil, Arguments{
Files: []string{
tp.testFiles["c.templ"].name,
},
FailIfChanged: true,
}); err != nil {
t.Fatalf("failed to run format command: %v", err)
}
data, err := os.ReadFile(tp.testFiles["c.templ"].name)
if err != nil {
t.Fatalf("failed to read file: %v", err)
}
if diff := cmp.Diff(tp.testFiles["c.templ"].expected, string(data)); diff != "" {
t.Error(diff)
}
})
}
22 changes: 21 additions & 1 deletion cmd/templ/fmtcmd/testdata.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ templ b() {
<div><p>B
</p></div>
}
-- a.templ --
-- b.templ --
package test

templ b() {
Expand All @@ -32,3 +32,23 @@ templ b() {
</p>
</div>
}
-- c.templ --
package test

templ c() {
<div>
<p>
C
</p>
</div>
}
-- c.templ --
package test

templ c() {
<div>
<p>
C
</p>
</div>
}

0 comments on commit b367dcb

Please sign in to comment.