Skip to content

Commit

Permalink
v.util: improve color_compare_files 2nd try, add test (#21247)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Apr 11, 2024
1 parent 39de87c commit d33ad62
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
36 changes: 18 additions & 18 deletions vlib/v/util/diff/diff.v
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ pub fn find_working_diff_command() !string {
return error('could not find specified VDIFF_TOOL ${diffcmd}')
}
if p.exit_code == 0 { // success
if diffcmd in ['gdiff', 'diff'] {
if p.output.contains('GNU diffutils') && env_diffopts == '' {
return '${diffcmd} --color=always'
// TODO: proper implemenation of --color flag
$if !macos {
if diffcmd in ['gdiff', 'diff'] {
if p.output.contains('GNU diffutils') && env_diffopts == '' {
return '${diffcmd} --color=always'
}
}
}
if diffcmd in ['code', 'code.cmd'] {
Expand All @@ -54,22 +57,19 @@ pub fn find_working_diff_command() !string {
return error('No working "diff" command found')
}

pub fn color_compare_files(diff_cmd string, file1 string, file2 string) string {
if diff_cmd != '' {
mut full_cmd := '${diff_cmd} --minimal --text --unified=2 --show-function-line="fn " ${os.quoted_path(file1)} ${os.quoted_path(file2)} '
$if freebsd {
full_cmd = '${diff_cmd} --minimal --text --unified=2 ${os.quoted_path(file1)} ${os.quoted_path(file2)} '
}
$if openbsd {
full_cmd = '${diff_cmd} -d -a -U 2 ${os.quoted_path(file1)} ${os.quoted_path(file2)} '
}
x := os.execute(full_cmd)
if x.exit_code < 0 {
return 'comparison command: `${full_cmd}` not found'
}
return x.output.trim_right('\r\n')
pub fn color_compare_files(diff_cmd string, path1 string, path2 string) string {
os.find_abs_path_of_executable(diff_cmd.all_before(' ')) or {
return 'comparison command: `${diff_cmd}` not found'
}
flags := $if openbsd {
['-d', '-a', '-U', '2']
} $else $if freebsd {
['--minimal', '--text', '--unified=2']
} $else {
['--minimal', '--text', '--unified=2', '--show-function-line="fn "']
}
return ''
full_cmd := '${diff_cmd} ${flags.join(' ')} ${os.quoted_path(path1)} ${os.quoted_path(path2)}'
return os.execute(full_cmd).output.trim_right('\r\n')
}

pub fn color_compare_strings(diff_cmd string, unique_prefix string, expected string, found string) string {
Expand Down
62 changes: 62 additions & 0 deletions vlib/v/util/diff/diff_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import v.util.diff
import os

fn test_compare_files() {
os.find_abs_path_of_executable('diff') or {
eprintln('> skipping test, since this test requires `diff` to be installed.')
return
}

f1 := "Module{
name: 'Foo'
description: 'Awesome V module.'
version: '0.0.0'
dependencies: []
}
"
f2 := "Module{
name: 'foo'
description: 'Awesome V module.'
version: '0.1.0'
license: 'MIT'
dependencies: []
}
"
tdir := os.join_path(os.vtmp_dir(), 'diff_test')
os.mkdir_all(tdir)!
defer {
os.rmdir_all(tdir) or {}
}
p1 := os.join_path(tdir, 'f1.txt')
p2 := os.join_path(tdir, 'f2.txt')
os.write_file(p1, f1)!
os.write_file(p2, f2)!

mut res := diff.color_compare_files('diff', p1, p2)
assert res.contains("-\tname: 'Foo'"), res
assert res.contains("+\tname: 'foo'"), res
assert res.contains("-\tversion: '0.0.0'"), res
assert res.contains("+\tversion: '0.1.0'"), res
assert res.contains("+\tlicense: 'MIT'"), res

// Test adding a flag to the command.
res = diff.color_compare_files('diff --ignore-case', p1, p2)
assert !res.contains("+\tname: 'foo'"), res
assert res.contains("-\tversion: '0.0.0'"), res
assert res.contains("+\tversion: '0.1.0'"), res
assert res.contains("+\tlicense: 'MIT'"), res

// Test again using `find_working_diff_command()`.
res = diff.color_compare_files(diff.find_working_diff_command()!, p1, p2)
assert res.contains("-\tversion: '0.0.0'"), res
assert res.contains("+\tversion: '0.1.0'"), res
assert res.contains("+\tlicense: 'MIT'"), res

// Test adding a flag via env flag.
os.setenv('VDIFF_OPTIONS', '--ignore-case', true)
res = diff.color_compare_files(diff.find_working_diff_command()!, p1, p2)
assert !res.contains("+\tname: 'foo'"), res
assert res.contains("-\tversion: '0.0.0'"), res
assert res.contains("+\tversion: '0.1.0'"), res
assert res.contains("+\tlicense: 'MIT'"), res
}

0 comments on commit d33ad62

Please sign in to comment.