11import v.util.diff
22import os
33
4- fn test_compare_files () {
4+ const tdir = os.join_path (os.vtmp_dir (), 'diff_test' )
5+
6+ fn testsuite_begin () {
57 os.find_abs_path_of_executable ('diff' ) or {
6- eprintln ('> skipping test, since this test requires `diff` to be installed. ' )
7- return
8+ eprintln ('> skipping test, since this test requires `diff` to be installed' )
9+ exit ( 0 )
810 }
11+ os.mkdir_all (tdir)!
12+ }
13+
14+ fn testsuite_end () {
15+ os.rmdir_all (tdir) or {}
16+ }
917
18+ fn test_compare_files () {
1019 f1 := "Module{
1120 name: 'Foo'
1221 description: 'Awesome V module.'
@@ -22,13 +31,8 @@ fn test_compare_files() {
2231 dependencies: []
2332}
2433"
25- tdir := os.join_path (os.vtmp_dir (), 'diff_test' )
26- os.mkdir_all (tdir)!
27- defer {
28- os.rmdir_all (tdir) or {}
29- }
30- p1 := os.join_path (tdir, 'f1.txt' )
31- p2 := os.join_path (tdir, 'f2.txt' )
34+ p1 := os.join_path (tdir, '${@FN} _f1.txt' )
35+ p2 := os.join_path (tdir, '${@FN} _f2.txt' )
3236 os.write_file (p1 , f1 )!
3337 os.write_file (p2 , f2 )!
3438
@@ -47,16 +51,34 @@ fn test_compare_files() {
4751 assert res.contains ("+\t license: 'MIT'" ), res
4852
4953 // Test again using `find_working_diff_command()`.
54+ os.setenv ('VDIFF_TOOL' , 'diff' , true )
5055 res = diff.color_compare_files (diff.find_working_diff_command ()! , p1 , p2 )
5156 assert res.contains ("-\t version: '0.0.0'" ), res
5257 assert res.contains ("+\t version: '0.1.0'" ), res
5358 assert res.contains ("+\t license: 'MIT'" ), res
5459
5560 // Test adding a flag via env flag.
56- os.setenv ('VDIFF_OPTIONS' , '--ignore-case' , true )
61+ os.setenv ('VDIFF_OPTIONS' , '--ignore-case' , true ) // ignored, when VDIFF_TOOL is not explicitly set
5762 res = diff.color_compare_files (diff.find_working_diff_command ()! , p1 , p2 )
5863 assert ! res.contains ("+\t name: 'foo'" ), res
5964 assert res.contains ("-\t version: '0.0.0'" ), res
6065 assert res.contains ("+\t version: '0.1.0'" ), res
6166 assert res.contains ("+\t license: 'MIT'" ), res
6267}
68+
69+ fn test_coloring () {
70+ if os.execute ('diff --color=always' ).output.starts_with ('diff: unrecognized option' ) {
71+ eprintln ('> skipping test, since `diff` does not support --color=always' )
72+ return
73+ }
74+ f1 := 'abc\n '
75+ f2 := 'abcd\n '
76+ p1 := os.join_path (tdir, '${@FN} _f1.txt' )
77+ p2 := os.join_path (tdir, '${@FN} _f2.txt' )
78+ os.write_file (p1 , f1 )!
79+ os.write_file (p2 , f2 )!
80+ res := diff.color_compare_files ('diff' , p1 , p2 )
81+ esc := rune (27 )
82+ assert res.contains ('${esc} [31m-abc${esc} [' ), res
83+ assert res.contains ('${esc} [32m+abcd${esc} [' ), res
84+ }
0 commit comments