@@ -1536,6 +1536,231 @@ func TestGit(t *testing.T) {
1536
1536
)
1537
1537
}
1538
1538
1539
+ func TestJujutsu (t * testing.T ) {
1540
+ as := require .New (t )
1541
+
1542
+ tempDir := test .TempExamples (t )
1543
+ configPath := filepath .Join (tempDir , "/treefmt.toml" )
1544
+
1545
+ test .ChangeWorkDir (t , tempDir )
1546
+
1547
+ // basic config
1548
+ cfg := & config.Config {
1549
+ FormatterConfigs : map [string ]* config.Formatter {
1550
+ "echo" : {
1551
+ Command : "echo" , // will not generate any underlying changes in the file
1552
+ Includes : []string {"*" },
1553
+ },
1554
+ },
1555
+ }
1556
+
1557
+ test .WriteConfig (t , configPath , cfg )
1558
+
1559
+ // init a jujutsu repo (disable signing for testing)
1560
+ jjCmd := exec .Command ("jj" , "git" , "init" , "--config" , "signing.backend=none" )
1561
+ as .NoError (jjCmd .Run (), "failed to init jujutsu repository" )
1562
+
1563
+ // run before adding anything to the index
1564
+ // we shouldn't pick up untracked files, because here the jujutsu walker differs from the git walker
1565
+ treefmt (t ,
1566
+ withConfig (configPath , cfg ),
1567
+ withNoError (t ),
1568
+ withStats (t , map [stats.Type ]int {
1569
+ stats .Traversed : 0 ,
1570
+ stats .Matched : 0 ,
1571
+ stats .Formatted : 0 ,
1572
+ stats .Changed : 0 ,
1573
+ }),
1574
+ )
1575
+
1576
+ // add everything to the index (disable signing for testing)
1577
+ jjCmd = exec .Command ("jj" , "file" , "track" , "--config" , "signing.backend=none" , "." )
1578
+ as .NoError (jjCmd .Run (), "failed to add everything to the index" )
1579
+
1580
+ // update jujutsu's index (disable signing for testing)
1581
+ jjCmd = exec .Command ("jj" , "--config" , "signing.backend=none" )
1582
+ as .NoError (jjCmd .Run (), "failed to update the index" )
1583
+
1584
+ // This is our first pass, since previously the files were not in the index. This should format all files.
1585
+ treefmt (t ,
1586
+ withConfig (configPath , cfg ),
1587
+ withNoError (t ),
1588
+ withStats (t , map [stats.Type ]int {
1589
+ stats .Traversed : 32 ,
1590
+ stats .Matched : 32 ,
1591
+ stats .Formatted : 32 ,
1592
+ stats .Changed : 0 ,
1593
+ }),
1594
+ )
1595
+
1596
+ // create a file which should be in .gitignore
1597
+ f , err := os .CreateTemp (tempDir , "test-*.txt" )
1598
+ as .NoError (err , "failed to create temp file" )
1599
+
1600
+ // add everything to the index (disable signing for testing), this command shoud ignore files in .gitignore
1601
+ jjCmd = exec .Command ("jj" , "file" , "track" , "--config" , "signing.backend=none" , "." )
1602
+ as .NoError (jjCmd .Run (), "failed to add everything to the index" )
1603
+
1604
+ // update jujutsu's index (disable signing for testing)
1605
+ jjCmd = exec .Command ("jj" , "--config" , "signing.backend=none" )
1606
+ as .NoError (jjCmd .Run (), "failed to update the index" )
1607
+
1608
+ t .Cleanup (func () {
1609
+ _ = f .Close ()
1610
+ })
1611
+
1612
+ treefmt (t ,
1613
+ withConfig (configPath , cfg ),
1614
+ withNoError (t ),
1615
+ withStats (t , map [stats.Type ]int {
1616
+ stats .Traversed : 32 ,
1617
+ stats .Matched : 32 ,
1618
+ stats .Formatted : 0 ,
1619
+ stats .Changed : 0 ,
1620
+ }),
1621
+ )
1622
+
1623
+ // remove python directory
1624
+ as .NoError (os .RemoveAll (filepath .Join (tempDir , "python" )), "failed to remove python directory" )
1625
+
1626
+ // update jujutsu's index (disable signing for testing)
1627
+ jjCmd = exec .Command ("jj" , "--config" , "signing.backend=none" )
1628
+ as .NoError (jjCmd .Run (), "failed to update the index" )
1629
+
1630
+ // we should traverse and match against fewer files, but no formatting should occur as no formatting signatures
1631
+ // are impacted
1632
+ treefmt (t ,
1633
+ withConfig (configPath , cfg ),
1634
+ withNoError (t ),
1635
+ withStats (t , map [stats.Type ]int {
1636
+ stats .Traversed : 29 ,
1637
+ stats .Matched : 29 ,
1638
+ stats .Formatted : 0 ,
1639
+ stats .Changed : 0 ,
1640
+ }),
1641
+ )
1642
+
1643
+ // remove nixpkgs.toml from the filesystem but leave it in the index
1644
+ as .NoError (os .Remove (filepath .Join (tempDir , "nixpkgs.toml" )))
1645
+
1646
+ // update jujutsu's index (disable signing for testing)
1647
+ jjCmd = exec .Command ("jj" , "--config" , "signing.backend=none" )
1648
+ as .NoError (jjCmd .Run (), "failed to update the index" )
1649
+
1650
+ // walk with filesystem instead of with jujutsu
1651
+ // the .jj folder contains 104 additional files
1652
+ // when added to the 30 we started with (34 minus nixpkgs.toml which we removed from the filesystem), we should
1653
+ // traverse 134 files.
1654
+ treefmt (t ,
1655
+ withArgs ("--walk" , "filesystem" ),
1656
+ withConfig (configPath , cfg ),
1657
+ withNoError (t ),
1658
+ withStats (t , map [stats.Type ]int {
1659
+ stats .Traversed : 134 ,
1660
+ stats .Matched : 134 ,
1661
+ stats .Formatted : 106 , // the echo formatter should only be applied to the new files
1662
+ stats .Changed : 0 ,
1663
+ }),
1664
+ )
1665
+
1666
+ // format specific sub paths
1667
+ // we should traverse and match against those files, but without any underlying change to their files or their
1668
+ // formatting config, we will not format them
1669
+
1670
+ treefmt (t ,
1671
+ withArgs ("go" ),
1672
+ withConfig (configPath , cfg ),
1673
+ withNoError (t ),
1674
+ withStats (t , map [stats.Type ]int {
1675
+ stats .Traversed : 2 ,
1676
+ stats .Matched : 2 ,
1677
+ stats .Formatted : 0 ,
1678
+ stats .Changed : 0 ,
1679
+ }),
1680
+ )
1681
+
1682
+ treefmt (t ,
1683
+ withArgs ("go" , "haskell" ),
1684
+ withConfig (configPath , cfg ),
1685
+ withNoError (t ),
1686
+ withStats (t , map [stats.Type ]int {
1687
+ stats .Traversed : 9 ,
1688
+ stats .Matched : 9 ,
1689
+ stats .Formatted : 0 ,
1690
+ stats .Changed : 0 ,
1691
+ }),
1692
+ )
1693
+
1694
+ treefmt (t ,
1695
+ withArgs ("-C" , tempDir , "go" , "haskell" , "ruby" ),
1696
+ withConfig (configPath , cfg ),
1697
+ withNoError (t ),
1698
+ withStats (t , map [stats.Type ]int {
1699
+ stats .Traversed : 10 ,
1700
+ stats .Matched : 10 ,
1701
+ stats .Formatted : 0 ,
1702
+ stats .Changed : 0 ,
1703
+ }),
1704
+ )
1705
+
1706
+ // try with a bad path
1707
+ treefmt (t ,
1708
+ withArgs ("-C" , tempDir , "haskell" , "foo" ),
1709
+ withConfig (configPath , cfg ),
1710
+ withError (func (as * require.Assertions , err error ) {
1711
+ as .ErrorContains (err , "foo not found" )
1712
+ }),
1713
+ )
1714
+
1715
+ // try with a path not in the git index
1716
+ _ , err = os .Create (filepath .Join (tempDir , "foo.txt" ))
1717
+ as .NoError (err )
1718
+
1719
+ // add everything to the index (disable signing for testing), this command shoud ignore files in .gitignore
1720
+ jjCmd = exec .Command ("jj" , "file" , "track" , "--config" , "signing.backend=none" , "." )
1721
+ as .NoError (jjCmd .Run (), "failed to add everything to the index" )
1722
+
1723
+ // update jujutsu's index (disable signing for testing)
1724
+ jjCmd = exec .Command ("jj" , "--config" , "signing.backend=none" )
1725
+ as .NoError (jjCmd .Run (), "failed to update the index" )
1726
+
1727
+ treefmt (t ,
1728
+ withArgs ("haskell" , "foo.txt" , "-vv" ),
1729
+ withConfig (configPath , cfg ),
1730
+ withNoError (t ),
1731
+ withStats (t , map [stats.Type ]int {
1732
+ stats .Traversed : 8 ,
1733
+ stats .Matched : 8 ,
1734
+ stats .Formatted : 1 , // we only format foo.txt, which is new to the cache
1735
+ stats .Changed : 0 ,
1736
+ }),
1737
+ )
1738
+
1739
+ treefmt (t ,
1740
+ withArgs ("go" , "foo.txt" ),
1741
+ withConfig (configPath , cfg ),
1742
+ withNoError (t ),
1743
+ withStats (t , map [stats.Type ]int {
1744
+ stats .Traversed : 3 ,
1745
+ stats .Matched : 3 ,
1746
+ stats .Formatted : 0 ,
1747
+ stats .Changed : 0 ,
1748
+ }),
1749
+ )
1750
+
1751
+ treefmt (t ,
1752
+ withArgs ("foo.txt" ),
1753
+ withConfig (configPath , cfg ),
1754
+ withNoError (t ),
1755
+ withStats (t , map [stats.Type ]int {
1756
+ stats .Traversed : 1 ,
1757
+ stats .Matched : 1 ,
1758
+ stats .Formatted : 0 ,
1759
+ stats .Changed : 0 ,
1760
+ }),
1761
+ )
1762
+ }
1763
+
1539
1764
func TestTreeRootCmd (t * testing.T ) {
1540
1765
as := require .New (t )
1541
1766
0 commit comments