-
-
Notifications
You must be signed in to change notification settings - Fork 260
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
Support custom path of a test file #1609
Open
josefpihrt
wants to merge
4
commits into
main
Choose a base branch
from
feature/test-file-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -295,7 +295,7 @@ private void VerifyAnnotations( | |||||
} | ||||||
|
||||||
internal static (Document document, ImmutableArray<ExpectedDocument> expectedDocuments) | ||||||
CreateDocument(Solution solution, string source, ImmutableArray<AdditionalFile> additionalFiles, TestOptions options, DiagnosticDescriptor? descriptor = null) | ||||||
CreateDocument(Solution solution, string source, string? path, ImmutableArray<AdditionalFile> additionalFiles, TestOptions options, DiagnosticDescriptor? descriptor = null) | ||||||
{ | ||||||
const string DefaultProjectName = "TestProject"; | ||||||
|
||||||
|
@@ -349,9 +349,9 @@ internal static (Document document, ImmutableArray<ExpectedDocument> expectedDoc | |||||
} | ||||||
|
||||||
Document document = project.AddDocument( | ||||||
options.DocumentName, | ||||||
Path.GetFileName(path) ?? options.DocumentName, | ||||||
SourceText.From(source), | ||||||
filePath: Path.Combine(directoryPath, options.DocumentName)); | ||||||
filePath: Path.Combine(directoryPath, path ?? options.DocumentName)); | ||||||
|
||||||
ImmutableArray<ExpectedDocument>.Builder? expectedDocuments = null; | ||||||
|
||||||
|
@@ -362,12 +362,12 @@ internal static (Document document, ImmutableArray<ExpectedDocument> expectedDoc | |||||
|
||||||
for (int i = 0; i < additionalFiles.Length; i++) | ||||||
{ | ||||||
string documentName = AppendNumberToFileName(options.DocumentName, i + 2); | ||||||
string documentName = Path.GetFileName(additionalFiles[i].Path) ?? AppendNumberToFileName(options.DocumentName, i + 2); | ||||||
|
||||||
Document additionalDocument = project.AddDocument( | ||||||
documentName, | ||||||
SourceText.From(additionalFiles[i].Source), | ||||||
filePath: Path.Combine(directoryPath, documentName)); | ||||||
filePath: Path.Combine(directoryPath, additionalFiles[i].Path ?? documentName)); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest
Suggested change
that should properly process passed paths without file name, like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added validation of file path |
||||||
|
||||||
string? expectedSource = additionalFiles[i].ExpectedSource; | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Roslynator.Testing; | ||
|
||
internal static class FilePathVerifier | ||
{ | ||
private static readonly char[] _invalidPathChars = Path.GetInvalidPathChars(); | ||
private static readonly char[] _invalidFileNameChars = Path.GetInvalidFileNameChars(); | ||
|
||
public static void VerifyFilePath(string? path) | ||
{ | ||
if (path is null) | ||
return; | ||
|
||
if (string.IsNullOrWhiteSpace(path)) | ||
throw new ArgumentException("File path cannot be empty or contain only whitespace", nameof(path)); | ||
|
||
if (path.IndexOfAny(_invalidPathChars) >= 0) | ||
throw new ArgumentException("File path contains invalid character(s)", nameof(path)); | ||
|
||
if (path[path.Length - 1] == Path.DirectorySeparatorChar | ||
|| path[path.Length - 1] == Path.AltDirectorySeparatorChar) | ||
{ | ||
throw new ArgumentException("File path cannot end with directory separator", nameof(path)); | ||
} | ||
|
||
var dirSeparatorIndex = FileSystemHelpers.LastIndexOfDirectorySeparator(path); | ||
if (dirSeparatorIndex >= 0) | ||
{ | ||
for (int i = dirSeparatorIndex + 1; i < path.Length; i++) | ||
{ | ||
if (_invalidFileNameChars.Contains(path[i])) | ||
throw new ArgumentException("File name contains invalid character(s)", nameof(path)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in case I am providing only a path, it could work erroneously. I do not know how it would behave for cases
Foo/Bar
andFoo/Bar/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added validation of file path