Skip to content
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
39 changes: 39 additions & 0 deletions THIRD-PARTY-NOTICES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Third-Party Notices

SpecHygiene incorporates material from the following third-party projects.

---

## Cucumber Gherkin — `gherkin-languages.json`

- **Used in:** `src/SpecHygiene/Analysis/Gherkin/gherkin-languages.json` (embedded as a resource and
shipped in the SpecHygiene NuGet package).
- **Source:** https://github.com/cucumber/gherkin
- **File:** https://github.com/cucumber/gherkin/blob/main/gherkin-languages.json
- **Pulled from commit:** `5bfc43c74e5a1f12f40f476105de0ef48743bd80`
(to re-sync the dialect table, replace the file from this path at a newer commit and update this SHA).
- **License:** MIT

```
MIT License

Copyright (c) 2017 Cucumber Ltd, Gaspar Nagy, Björn Rasmusson, Peter Sergeant, and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
47 changes: 27 additions & 20 deletions src/SpecHygiene/Analysis/DuplicateAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ public class DuplicateAnalyzer
private readonly HashSet<string> _reviewedFingerprints;
private readonly StepNormalizer _normalizer;
private readonly DataSourceResolver _dataSourceResolver;
private readonly GherkinDialectProvider _dialectProvider;

private static readonly Regex PlaceholderRegex = new(@"<([^>]+)>", RegexOptions.Compiled);
private static readonly Regex ValidPlaceholderNameRegex = new(@"^[a-zA-Z_][a-zA-Z0-9_]*$", RegexOptions.Compiled);

public DuplicateAnalyzer(
AnalyzerSettings settings,
HashSet<string>? reviewedFingerprints = null,
DataSourceResolver? dataSourceResolver = null)
DataSourceResolver? dataSourceResolver = null,
GherkinDialectProvider? dialectProvider = null)
{
_settings = settings;
_reviewedFingerprints = reviewedFingerprints ?? new HashSet<string>();
_normalizer = new StepNormalizer();
_dataSourceResolver = dataSourceResolver ?? new DataSourceResolver(settings.Analysis.DataSource);
_dialectProvider = dialectProvider ?? GherkinDialectProvider.Default;
}

/// <summary>
Expand Down Expand Up @@ -145,6 +148,15 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
try
{
var lines = await File.ReadAllLinesAsync(filePath);

// Resolve the file's Gherkin dialect from its "# language:" header (English if absent).
// For English we keep honoring the user-configured StepKeywords (unchanged behavior);
// for a localized file we use that dialect's own step keywords.
var dialect = _dialectProvider.Detect(lines);
var stepKeywords = dialect.Language.Equals(GherkinDialectProvider.DefaultLanguage, StringComparison.OrdinalIgnoreCase)
? _settings.Analysis.StepKeywords
: dialect.StepKeywords;

var currentScenario = "";
var currentScenarioLine = 0;
var isScenarioOutline = false;
Expand Down Expand Up @@ -184,7 +196,7 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
}

// Track Background
if (line.StartsWith("Background:", StringComparison.OrdinalIgnoreCase))
if (dialect.IsBackground(line))
{
inBackground = true;
backgroundSteps.Clear();
Expand All @@ -196,9 +208,7 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
}

// Track Scenario or Scenario Outline
if (line.StartsWith("Scenario Outline:", StringComparison.OrdinalIgnoreCase) ||
line.StartsWith("Scenario Template:", StringComparison.OrdinalIgnoreCase) ||
line.StartsWith("Scenario:", StringComparison.OrdinalIgnoreCase))
if (dialect.IsScenarioStart(line))
{
inBackground = false;

Expand All @@ -221,8 +231,7 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
continue;
}

isScenarioOutline = line.StartsWith("Scenario Outline:", StringComparison.OrdinalIgnoreCase) ||
line.StartsWith("Scenario Template:", StringComparison.OrdinalIgnoreCase);
isScenarioOutline = dialect.IsScenarioOutline(line);

currentScenario = line.Split(':', 2).LastOrDefault()?.Trim() ?? "";
currentScenarioLine = i + 1;
Expand All @@ -247,7 +256,7 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
}

// Get Examples info - use tuple deconstruction
var (placeholders, hasExamples) = FindExamplesInfo(lines, i);
var (placeholders, hasExamples) = FindExamplesInfo(lines, i, dialect);
examplesPlaceholders = placeholders;
hasExamplesSection = hasExamples;

Expand Down Expand Up @@ -311,8 +320,8 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
}

// Skip Examples, Feature lines etc.
if (line.StartsWith("Examples:", StringComparison.OrdinalIgnoreCase) ||
line.StartsWith("Feature:", StringComparison.OrdinalIgnoreCase) ||
if (dialect.IsExamples(line) ||
dialect.IsFeature(line) ||
line.StartsWith("|") ||
string.IsNullOrWhiteSpace(line) ||
line.StartsWith("#"))
Expand All @@ -321,7 +330,7 @@ public async Task<DuplicateAnalysisReport> AnalyzeAsync(bool skipDuplicateMatchi
}

// Check if this is a step line
if (IsStepLine(line))
if (IsStepLine(line, stepKeywords))
{
var stepText = line;
var dataTable = new List<Dictionary<string, string>>();
Expand Down Expand Up @@ -1179,16 +1188,16 @@ private List<string> GetProjectFolders(string root)
return Directory.GetDirectories(root).ToList();
}

private (HashSet<string> Placeholders, bool HasExamples) FindExamplesInfo(string[] lines, int startIndex)
private (HashSet<string> Placeholders, bool HasExamples) FindExamplesInfo(string[] lines, int startIndex, GherkinDialect dialect)
{
var placeholders = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var hasExamples = false;

for (int i = startIndex + 1; i < lines.Length; i++)
{
var line = lines[i].Trim();
if (line.StartsWith("Examples:", StringComparison.OrdinalIgnoreCase))

if (dialect.IsExamples(line))
{
hasExamples = true;

Expand All @@ -1215,9 +1224,7 @@ private List<string> GetProjectFolders(string root)
}

// Stop at next scenario
if (line.StartsWith("Scenario:", StringComparison.OrdinalIgnoreCase) ||
line.StartsWith("Scenario Outline:", StringComparison.OrdinalIgnoreCase) ||
line.StartsWith("Scenario Template:", StringComparison.OrdinalIgnoreCase))
if (dialect.IsScenarioStart(line))
{
break;
}
Expand All @@ -1226,9 +1233,9 @@ private List<string> GetProjectFolders(string root)
return (placeholders, hasExamples);
}

private bool IsStepLine(string line)
private bool IsStepLine(string line, IReadOnlyList<string> stepKeywords)
{
return _settings.Analysis.StepKeywords.Any(k =>
return stepKeywords.Any(k =>
line.StartsWith(k + " ", StringComparison.OrdinalIgnoreCase));
}

Expand Down
Loading
Loading