Merge pull request #19199 from michaelnebel/csharp/synctestfiles

C#: Accept file sync mismatch for C# testfiles.
This commit is contained in:
Michael Nebel
2025-04-04 10:07:05 +02:00
committed by GitHub
7 changed files with 50 additions and 21 deletions

View File

@@ -58,7 +58,19 @@ def file_checksum(filename):
with open(filename, 'rb') as file_handle:
return hashlib.sha1(file_handle.read()).hexdigest()
def check_group(group_name, files, master_file_picker, emit_error):
def accept_prefix(line1, line2):
suffix = line2.removeprefix(line1)
return not suffix or suffix.lstrip().startswith("//")
def equivalent_lines(lines1, lines2):
if len(lines1) != len(lines2):
return False
for line1, line2 in zip(lines1, lines2):
if not accept_prefix(line1, line2) and not accept_prefix(line2, line1):
return False
return True
def check_group(group_name, files, master_file_picker, emit_error, accept_prefix):
extant_files = [f for f in files if path.isfile(f)]
if len(extant_files) == 0:
emit_error(__file__, 0, "No files found from group '" + group_name + "'.")
@@ -70,11 +82,23 @@ def check_group(group_name, files, master_file_picker, emit_error):
return
checksums = {file_checksum(f) for f in extant_files}
if len(checksums) == 1 and len(extant_files) == len(files):
same_lengths = len(extant_files) == len(files)
if len(checksums) == 1 and same_lengths:
# All files are present and identical.
return
# In this case we also consider files indentical, if
# (1) The group only containts two files.
# (2) The lines of one file are the same as the lines of another file
# modulo comments.
if accept_prefix and same_lengths and len(extant_files) == 2:
with open(extant_files[0], 'r') as f1:
file1_lines = [l.strip('\n\r') for l in f1.readlines()]
with open(extant_files[1], 'r') as f2:
file2_lines = [l.strip('\n\r') for l in f2.readlines()]
if equivalent_lines(file1_lines, file2_lines):
return
master_file = master_file_picker(extant_files)
if master_file is None:
emit_error(__file__, 0,
@@ -139,9 +163,10 @@ def sync_identical_files(emit_error):
raise Exception("Bad command line or file not found")
chdir_repo_root()
load_if_exists('.', 'config/identical-files.json')
file_groups.update(csharp_test_files())
for group_name, files in csharp_test_files().items():
check_group(group_name, files, master_file_picker, emit_error, True)
for group_name, files in file_groups.items():
check_group(group_name, files, master_file_picker, emit_error)
check_group(group_name, files, master_file_picker, emit_error, False)
def main():
sync_identical_files(emit_local_error)

View File

@@ -114,15 +114,6 @@ class Test
public void Dispose() { }
}
class Bad
{
long GetLength(string file)
{
var stream = new FileStream(file, FileMode.Open); // $ Alert
return stream.Length;
}
}
static class Extensions
{
public static FileStream Fluent(this FileStream fs) => fs;

View File

@@ -4,4 +4,4 @@
| NoDisposeCallOnLocalIDisposable.cs:76:25:76:71 | call to method Create | Disposable 'XmlReader' is created but not disposed. |
| NoDisposeCallOnLocalIDisposable.cs:76:42:76:64 | object creation of type StringReader | Disposable 'StringReader' is created but not disposed. |
| NoDisposeCallOnLocalIDisposable.cs:104:23:104:38 | object creation of type HttpClient | Disposable 'HttpClient' is created but not disposed. |
| NoDisposeCallOnLocalIDisposable.cs:121:22:121:56 | object creation of type FileStream | Disposable 'FileStream' is created but not disposed. |
| NoDisposeCallOnLocalIDisposableBad.cs:8:22:8:56 | object creation of type FileStream | Disposable 'FileStream' is created but not disposed. |

View File

@@ -0,0 +1,11 @@
using System;
using System.IO;
class Bad
{
long GetLength(string file)
{
var stream = new FileStream(file, FileMode.Open); // $ Alert
return stream.Length;
}
}

View File

@@ -11,6 +11,7 @@
| ConstantCondition.cs:114:13:114:14 | access to parameter b1 | Condition always evaluates to 'true'. |
| ConstantCondition.cs:114:19:114:20 | access to parameter b2 | Condition always evaluates to 'true'. |
| ConstantCondition.cs:141:22:141:22 | _ | Pattern always matches. |
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |
| ConstantConditionalExpressionCondition.cs:13:21:13:30 | ... == ... | Condition always evaluates to 'true'. |
@@ -19,7 +20,6 @@
| ConstantIfCondition.cs:11:17:11:29 | ... == ... | Condition always evaluates to 'true'. |
| ConstantIfCondition.cs:14:17:14:21 | false | Condition always evaluates to 'false'. |
| ConstantIfCondition.cs:17:17:17:26 | ... == ... | Condition always evaluates to 'true'. |
| ConstantIfCondition.cs:30:20:30:24 | ... > ... | Condition always evaluates to 'false'. |
| ConstantIsNullOrEmpty.cs:10:21:10:54 | call to method IsNullOrEmpty | Condition always evaluates to 'false'. |
| ConstantIsNullOrEmpty.cs:46:21:46:46 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. |
| ConstantIsNullOrEmpty.cs:50:21:50:44 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. |

View File

@@ -0,0 +1,7 @@
class Bad
{
public int Max(int a, int b)
{
return a > a ? a : b; // $ Alert
}
}

View File

@@ -25,11 +25,6 @@ namespace ConstantIfCondition
}
}
public int Max(int a, int b)
{
return a > a ? a : b; // $ Alert
}
public int Bar()
{
return ZERO;