-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add initial part * Disable codefix test as the fix breaks the tests * Improve error message & taking in account bin/obj folder * Fix unit test * Fix SonarCloud remark * Update to latest version of FileSystem NuGet * Using the CICD.Parsers * CR remark
- Loading branch information
1 parent
a7797b2
commit 245a222
Showing
33 changed files
with
800 additions
and
8 deletions.
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
56 changes: 56 additions & 0 deletions
56
Protocol/Error Messages/Protocol/QActions/QAction/CheckFileEncoding.cs
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,56 @@ | ||
// <auto-generated>This is auto-generated code by Validator Management Tool. Do not modify.</auto-generated> | ||
namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.QActions.QAction.CheckFileEncoding | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Skyline.DataMiner.CICD.Models.Protocol.Read; | ||
using Skyline.DataMiner.CICD.Validators.Common.Interfaces; | ||
using Skyline.DataMiner.CICD.Validators.Common.Model; | ||
using Skyline.DataMiner.CICD.Validators.Protocol.Common; | ||
using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces; | ||
|
||
internal static class Error | ||
{ | ||
public static IValidationResult InvalidFileEncoding(IValidate test, IReadable referenceNode, IReadable positionNode, string invalidFileEncoding, string fileName, string qactionId) | ||
{ | ||
return new ValidationResult | ||
{ | ||
Test = test, | ||
CheckId = CheckId.CheckFileEncoding, | ||
ErrorId = ErrorIds.InvalidFileEncoding, | ||
FullId = "3.40.1", | ||
Category = Category.QAction, | ||
Severity = Severity.Minor, | ||
Certainty = Certainty.Certain, | ||
Source = Source.Validator, | ||
FixImpact = FixImpact.NonBreaking, | ||
GroupDescription = "", | ||
Description = String.Format("Invalid file encoding '{0}' detected in file '{1}'. QAction ID '{2}'.", invalidFileEncoding, fileName, qactionId), | ||
HowToFix = "", | ||
ExampleCode = "", | ||
Details = "Each file in a QAction needs to be UTF-8 as otherwise certain characters could be converted to invalid characters.", | ||
HasCodeFix = true, | ||
|
||
PositionNode = positionNode, | ||
ReferenceNode = referenceNode, | ||
}; | ||
} | ||
} | ||
|
||
internal static class ErrorIds | ||
{ | ||
public const uint InvalidFileEncoding = 1; | ||
} | ||
|
||
/// <summary> | ||
/// Contains the identifiers of the checks. | ||
/// </summary> | ||
public static class CheckId | ||
{ | ||
/// <summary> | ||
/// The check identifier. | ||
/// </summary> | ||
public const uint CheckFileEncoding = 40; | ||
} | ||
} |
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
124 changes: 124 additions & 0 deletions
124
Protocol/Tests/Protocol/QActions/QAction/CheckFileEncoding.cs
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,124 @@ | ||
namespace Skyline.DataMiner.CICD.Validators.Protocol.Tests.Protocol.QActions.QAction.CheckFileEncoding | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
using Skyline.DataMiner.CICD.FileSystem; | ||
using Skyline.DataMiner.CICD.Models.Protocol; | ||
using Skyline.DataMiner.CICD.Models.Protocol.Read; | ||
using Skyline.DataMiner.CICD.Parsers.Common.VisualStudio.Projects; | ||
using Skyline.DataMiner.CICD.Validators.Common.Interfaces; | ||
using Skyline.DataMiner.CICD.Validators.Common.Model; | ||
using Skyline.DataMiner.CICD.Validators.Protocol.Common; | ||
using Skyline.DataMiner.CICD.Validators.Protocol.Common.Attributes; | ||
using Skyline.DataMiner.CICD.Validators.Protocol.Common.Extensions; | ||
using Skyline.DataMiner.CICD.Validators.Protocol.Interfaces; | ||
|
||
[Test(CheckId.CheckFileEncoding, Category.QAction)] | ||
internal class CheckFileEncoding : IValidate, ICodeFix/*, ICompare*/ | ||
{ | ||
public List<IValidationResult> Validate(ValidatorContext context) | ||
{ | ||
List<IValidationResult> results = new List<IValidationResult>(); | ||
|
||
if (!context.HasQActionsAndIsSolution) | ||
{ | ||
// Early skip when no QActions are present or when it is not solution based. | ||
return results; | ||
} | ||
|
||
foreach ((CompiledQActionProject projectData, IQActionsQAction qaction) in context.EachQActionProject(true)) | ||
{ | ||
try | ||
{ | ||
Project project = Project.Load(projectData.Project.FilePath, projectData.Project.Name); | ||
string projectDirectory = FileSystem.Instance.Path.GetDirectoryName(project.Path); | ||
foreach (ProjectFile file in project.Files) | ||
{ | ||
string csharpFile = FileSystem.Instance.Path.Combine(projectDirectory, file.Name); | ||
using (StreamReader sr = new StreamReader(csharpFile)) | ||
{ | ||
// Have a look in the file, so it can detect the encoding. | ||
sr.Peek(); | ||
|
||
if (!sr.CurrentEncoding.Equals(Encoding.UTF8)) | ||
{ | ||
string fileName = FileSystem.Instance.Path.GetFileName(csharpFile); | ||
results.Add(Error.InvalidFileEncoding(this, qaction, qaction, sr.CurrentEncoding.EncodingName, fileName, qaction.Id.RawValue) | ||
.WithExtraData(ExtraData.InvalidFileEncoding, csharpFile)); | ||
} | ||
} | ||
} | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
// Can happen in legacy style projects. In case the file mentioned in the csproj does not exist. | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
public ICodeFixResult Fix(CodeFixContext context) | ||
{ | ||
CodeFixResult result = new CodeFixResult(); | ||
var fs = FileSystem.Instance; | ||
|
||
switch (context.Result.ErrorId) | ||
{ | ||
case ErrorIds.InvalidFileEncoding: | ||
if (context.Result.ExtraData.TryGetValue(ExtraData.InvalidFileEncoding, out object csharpFile) && | ||
csharpFile is string filePath && | ||
fs.File.Exists(filePath)) | ||
{ | ||
string tempPath = fs.Path.Combine(fs.Path.GetTempPath(), fs.Path.GetRandomFileName()); | ||
using (StreamReader sr = new StreamReader(filePath)) | ||
using (StreamWriter sw = new StreamWriter(tempPath, false, Encoding.UTF8)) | ||
{ | ||
// Have a look in the file, so it can detect the encoding. | ||
sr.Peek(); | ||
|
||
int charsRead; | ||
char[] buffer = new char[128 * 1024]; | ||
while ((charsRead = sr.ReadBlock(buffer, 0, buffer.Length)) > 0) | ||
{ | ||
sw.Write(buffer, 0, charsRead); | ||
} | ||
} | ||
|
||
fs.File.Delete(filePath); | ||
fs.File.Move(tempPath, filePath); | ||
|
||
result.Success = true; | ||
} | ||
else | ||
{ | ||
result.Message = "Unable to locate file."; | ||
} | ||
|
||
break; | ||
|
||
default: | ||
result.Message = $"This error ({context.Result.ErrorId}) isn't implemented."; | ||
break; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
////public List<IValidationResult> Compare(MajorChangeCheckContext context) | ||
////{ | ||
//// List<IValidationResult> results = new List<IValidationResult>(); | ||
|
||
//// return results; | ||
////} | ||
} | ||
|
||
internal enum ExtraData | ||
{ | ||
InvalidFileEncoding, | ||
} | ||
} |
Oops, something went wrong.