@@ -1531,6 +1531,118 @@ func TestGit(t *testing.T) {
1531
1531
)
1532
1532
}
1533
1533
1534
+ func TestTreeRootCmd (t * testing.T ) {
1535
+ as := require .New (t )
1536
+
1537
+ tempDir := test .TempExamples (t )
1538
+ configPath := filepath .Join (tempDir , "/treefmt.toml" )
1539
+
1540
+ test .ChangeWorkDir (t , tempDir )
1541
+
1542
+ // basic config
1543
+ cfg := & config.Config {
1544
+ FormatterConfigs : map [string ]* config.Formatter {
1545
+ "echo" : {
1546
+ Command : "echo" , // will not generate any underlying changes in the file
1547
+ Includes : []string {"*" },
1548
+ },
1549
+ },
1550
+ }
1551
+
1552
+ test .WriteConfig (t , configPath , cfg )
1553
+
1554
+ // construct a tree root command with some error logging and dumping output on stdout
1555
+ treeRootCmd := func (output string ) string {
1556
+ return fmt .Sprintf ("bash -c '>&2 echo -e \" some error text\n some more error text\" && echo %s'" , output )
1557
+ }
1558
+
1559
+ // helper for checking the contents of stderr matches our expected debug output
1560
+ checkStderr := func (buf []byte ) {
1561
+ output := string (buf )
1562
+ as .Contains (output , "DEBU tree-root-cmd | stderr: some error text\n " )
1563
+ as .Contains (output , "DEBU tree-root-cmd | stderr: some more error text\n " )
1564
+ }
1565
+
1566
+ // run treefmt with DEBUG logging enabled and with tree root cmd being the root of the temp directory
1567
+ treefmt (t ,
1568
+ withArgs ("-vv" , "--tree-root-cmd" , treeRootCmd (tempDir )),
1569
+ withNoError (t ),
1570
+ withStderr (checkStderr ),
1571
+ withConfig (configPath , cfg ),
1572
+ withStats (t , map [stats.Type ]int {
1573
+ stats .Traversed : 32 ,
1574
+ stats .Matched : 32 ,
1575
+ stats .Formatted : 32 ,
1576
+ stats .Changed : 0 ,
1577
+ }),
1578
+ )
1579
+
1580
+ // run from a subdirectory, mixing things up by specifying the command via an env variable
1581
+ treefmt (t ,
1582
+ withArgs ("-vv" ),
1583
+ withEnv (map [string ]string {
1584
+ "TREEFMT_TREE_ROOT_CMD" : treeRootCmd (filepath .Join (tempDir , "go" )),
1585
+ }),
1586
+ withNoError (t ),
1587
+ withStderr (checkStderr ),
1588
+ withConfig (configPath , cfg ),
1589
+ withStats (t , map [stats.Type ]int {
1590
+ stats .Traversed : 2 ,
1591
+ stats .Matched : 2 ,
1592
+ stats .Formatted : 2 ,
1593
+ stats .Changed : 0 ,
1594
+ }),
1595
+ )
1596
+
1597
+ // run from a subdirectory, mixing things up by specifying the command via config
1598
+ cfg .TreeRootCmd = treeRootCmd (filepath .Join (tempDir , "haskell" ))
1599
+
1600
+ treefmt (t ,
1601
+ withArgs ("-vv" ),
1602
+ withNoError (t ),
1603
+ withStderr (checkStderr ),
1604
+ withConfig (configPath , cfg ),
1605
+ withStats (t , map [stats.Type ]int {
1606
+ stats .Traversed : 7 ,
1607
+ stats .Matched : 7 ,
1608
+ stats .Formatted : 7 ,
1609
+ stats .Changed : 0 ,
1610
+ }),
1611
+ )
1612
+
1613
+ // run with a long-running command (2 seconds or more)
1614
+ treefmt (t ,
1615
+ withArgs (
1616
+ "-vv" ,
1617
+ "--tree-root-cmd" , fmt .Sprintf (
1618
+ "bash -c 'sleep 2 && echo %s'" ,
1619
+ tempDir ,
1620
+ ),
1621
+ ),
1622
+ withError (func (as * require.Assertions , err error ) {
1623
+ as .ErrorContains (err , "tree-root-cmd was killed after taking more than 2s to execute" )
1624
+ }),
1625
+ withConfig (configPath , cfg ),
1626
+ )
1627
+
1628
+ // run with a command that outputs multiple lines
1629
+ treefmt (t ,
1630
+ withArgs (
1631
+ "--tree-root-cmd" , fmt .Sprintf (
1632
+ "bash -c 'echo %s && echo %s'" ,
1633
+ tempDir , tempDir ,
1634
+ ),
1635
+ ),
1636
+ withStderr (func (buf []byte ) {
1637
+ as .Contains (string (buf ), fmt .Sprintf ("ERRO tree-root-cmd | stdout: \n %s\n %s\n " , tempDir , tempDir ))
1638
+ }),
1639
+ withError (func (as * require.Assertions , err error ) {
1640
+ as .ErrorContains (err , "tree-root-cmd cannot output multiple lines" )
1641
+ }),
1642
+ withConfig (configPath , cfg ),
1643
+ )
1644
+ }
1645
+
1534
1646
func TestTreeRootExclusivity (t * testing.T ) {
1535
1647
tempDir := test .TempExamples (t )
1536
1648
configPath := filepath .Join (tempDir , "/treefmt.toml" )
@@ -1552,7 +1664,7 @@ func TestTreeRootExclusivity(t *testing.T) {
1552
1664
1553
1665
assertExclusiveConfig := func (as * require.Assertions , err error ) {
1554
1666
as .ErrorContains (err ,
1555
- "only one of tree-root, tree-root-cmd or tree-root-file can be specified" ,
1667
+ "at most one of tree-root, tree-root-cmd or tree-root-file can be specified" ,
1556
1668
)
1557
1669
}
1558
1670
@@ -1580,7 +1692,7 @@ func TestTreeRootExclusivity(t *testing.T) {
1580
1692
},
1581
1693
}
1582
1694
1583
- permutations := [][]string {
1695
+ invalidCombinations := [][]string {
1584
1696
{"tree-root" , "tree-root-cmd" },
1585
1697
{"tree-root" , "tree-root-file" },
1586
1698
{"tree-root-cmd" , "tree-root-file" },
@@ -1591,11 +1703,11 @@ func TestTreeRootExclusivity(t *testing.T) {
1591
1703
// Given that ultimately everything is being reduced into the config object after parsing from viper, I'm fairly
1592
1704
// confident if these tests all pass then the mixed methods should yield the same result.
1593
1705
1594
- // test permutations of the same type
1595
- for _ , perm := range permutations {
1706
+ // for each set of invalid args, test them with flags, environment variables, and config entries.
1707
+ for _ , combination := range invalidCombinations {
1596
1708
// test flags
1597
1709
var args []string
1598
- for _ , key := range perm {
1710
+ for _ , key := range combination {
1599
1711
args = append (args , flagValues [key ]... )
1600
1712
}
1601
1713
@@ -1607,7 +1719,7 @@ func TestTreeRootExclusivity(t *testing.T) {
1607
1719
// test env variables
1608
1720
env := make (map [string ]string )
1609
1721
1610
- for _ , key := range perm {
1722
+ for _ , key := range combination {
1611
1723
entry := envValues [key ]
1612
1724
env [entry [0 ]] = entry [1 ]
1613
1725
}
@@ -1622,7 +1734,7 @@ func TestTreeRootExclusivity(t *testing.T) {
1622
1734
FormatterConfigs : formatterConfigs ,
1623
1735
}
1624
1736
1625
- for _ , key := range perm {
1737
+ for _ , key := range combination {
1626
1738
entry := configValues [key ]
1627
1739
entry (cfg )
1628
1740
}
0 commit comments