Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.utils: allow to set names of compared items when diffing strings #21650

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions vlib/v/util/diff/diff.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ pub:
env_overwrite_var ?string = 'VDIFF_CMD'
}

@[params]
pub struct CompareTextOptions {
CompareOptions
pub:
base_name string = 'base'
target_name string = 'target'
}

// Default options for `diff` and `colordiff`.
// Short `diff` args are supported more widely (e.g. on OpenBSD, ref. https://man.openbsd.org/diff.1).
// `-d -a -U 2` ^= `--minimal --text --unified=2`
Expand Down Expand Up @@ -79,15 +87,15 @@ pub fn compare_files(path1 string, path2 string, opts CompareOptions) !string {
}

// compare_text returns a string displaying the differences between two strings.
pub fn compare_text(text1 string, text2 string, opts CompareOptions) !string {
pub fn compare_text(text1 string, text2 string, opts CompareTextOptions) !string {
ctime := time.sys_mono_now()
tmp_dir := os.join_path_single(os.vtmp_dir(), ctime.str())
os.mkdir(tmp_dir)!
defer {
os.rmdir_all(tmp_dir) or {}
}
path1 := os.join_path_single(tmp_dir, 'text1.txt')
path2 := os.join_path_single(tmp_dir, 'text2.txt')
path1 := os.join_path_single(tmp_dir, opts.base_name)
path2 := os.join_path_single(tmp_dir, opts.target_name)
// When comparing strings and not files, prevent `\ No newline at end of file` in the output.
if !text1.ends_with('\n') || !text2.ends_with('\n') {
os.write_file(path1, text1 + '\n')!
Expand All @@ -96,7 +104,7 @@ pub fn compare_text(text1 string, text2 string, opts CompareOptions) !string {
os.write_file(path1, text1)!
os.write_file(path2, text2)!
}
return compare_files(path1, path2, opts)!
return compare_files(path1, path2, opts.CompareOptions)!
}

fn (opts CompareOptions) find_tool() !(DiffTool, string) {
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/util/diff/diff_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ fn test_compare_string() {
assert res.contains('-abc'), res
assert res.contains('+abcd'), res
assert !res.contains('No newline at end of file'), res
// Default base and target name.
assert res.match_glob('*---*base*'), res
assert res.match_glob('*+++*target*'), res
// Custom base and target name.
res = diff.compare_text('abc', 'abcd', tool: .diff, base_name: 'old.v', target_name: 'new.v')!
assert res.match_glob('*---*old.v*'), res
assert res.match_glob('*+++*new.v*'), res
}

fn test_coloring() {
Expand Down
Loading