Skip to content

Commit f6cb995

Browse files
authored
add integration test exercising a push with only a delete. updating logic to include deleted files in the detect pending changes. something that was erroneously excluded when adding security scanning to the project (#8681)
1 parent 60925ca commit f6cb995

3 files changed

Lines changed: 106 additions & 31 deletions

File tree

tools/test-proxy/Azure.Sdk.Tools.TestProxy.Tests/IntegrationTests/GitStoreIntegrationPushTests.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,85 @@ public async Task Scenario5(string inputJson)
506506
}
507507
}
508508

509+
510+
/// <summary>
511+
/// 1. Restore from the initial tag in pull/scenarios
512+
/// 2. Only delete a file
513+
/// 3. Push to new branch
514+
/// 4. Verify local files are what is expected
515+
/// 5. Verify assets.json was updated with the new commit Tag
516+
/// </summary>
517+
/// <param name="inputJson"></param>
518+
/// <returns></returns>
519+
[EnvironmentConditionalSkipTheory]
520+
[InlineData(
521+
@"{
522+
""AssetsRepo"": ""Azure/azure-sdk-assets-integration"",
523+
""AssetsRepoPrefixPath"": ""pull/scenarios"",
524+
""AssetsRepoId"": """",
525+
""TagPrefix"": ""language/tables"",
526+
""Tag"": ""python/tables_fc54d0""
527+
}")]
528+
[Trait("Category", "Integration")]
529+
public async Task EnsureOnlyDeletesDetected(string inputJson)
530+
{
531+
var folderStructure = new string[]
532+
{
533+
GitStoretests.AssetsJson
534+
};
535+
536+
Assets assets = JsonSerializer.Deserialize<Assets>(inputJson);
537+
Assets updatedAssets = null;
538+
string originalTagPrefix = assets.TagPrefix;
539+
string originalTag = assets.Tag;
540+
var testFolder = TestHelpers.DescribeTestFolder(assets, folderStructure, isPushTest: true);
541+
try
542+
{
543+
// Ensure that the Tag was updated
544+
Assert.NotEqual(originalTag, assets.TagPrefix);
545+
546+
var jsonFileLocation = Path.Join(testFolder, GitStoretests.AssetsJson);
547+
548+
var parsedConfiguration = await _defaultStore.ParseConfigurationFile(jsonFileLocation);
549+
await _defaultStore.Restore(jsonFileLocation);
550+
551+
// Calling Path.GetFullPath of the Path.Combine will ensure any directory separators are normalized for
552+
// the OS the test is running on. The reason being is that AssetsRepoPrefixPath, if there's a separator,
553+
// will be a forward one as expected by git but on Windows this won't result in a usable path.
554+
string localFilePath = Path.GetFullPath(Path.Combine(parsedConfiguration.AssetsRepoLocation.ToString(), parsedConfiguration.AssetsRepoPrefixPath.ToString()));
555+
556+
// These are the files pulled down with the original Tag
557+
Assert.Equal(3, System.IO.Directory.EnumerateFiles(localFilePath).Count());
558+
Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1));
559+
Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file2.txt", 1));
560+
Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1));
561+
562+
File.Delete(Path.Combine(localFilePath, "file2.txt"));
563+
564+
// Push the update, it should be a clean push
565+
await _defaultStore.Push(jsonFileLocation);
566+
567+
// Verify that after the push the directory still contains our updated files
568+
Assert.Equal(2, System.IO.Directory.EnumerateFiles(localFilePath).Count());
569+
Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file1.txt", 1));
570+
Assert.True(TestHelpers.VerifyFileVersion(localFilePath, "file3.txt", 1));
571+
572+
// Ensure that the config was updated with the new Tag as part of the push
573+
updatedAssets = TestHelpers.LoadAssetsFromFile(jsonFileLocation);
574+
Assert.NotEqual(originalTag, updatedAssets.Tag);
575+
576+
// Ensure that the targeted tag is present on the repo
577+
TestHelpers.CheckExistenceOfTag(updatedAssets, localFilePath);
578+
await TestHelpers.CheckBreadcrumbAgainstAssetsJsons(new string[] { jsonFileLocation });
579+
}
580+
finally
581+
{
582+
DirectoryHelper.DeleteGitDirectory(testFolder);
583+
TestHelpers.CleanupIntegrationTestTag(assets);
584+
TestHelpers.CleanupIntegrationTestTag(updatedAssets);
585+
}
586+
}
587+
509588
/// <summary>
510589
/// 1. Restore from a tag that has minimal existing files and a long destination path
511590
/// 2. Add a _bunch_ of files

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/SecretScanner.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,41 @@ public List<Tuple<string, Detection>> DiscoverSecrets(string assetRepoRoot, IEnu
2929
var detectedSecrets = new ConcurrentBag<Tuple<string, Detection>>();
3030
var total = relativePaths.Count();
3131
var seen = 0;
32-
Console.WriteLine(string.Empty);
3332

34-
var options = new ParallelOptions
33+
if (relativePaths.Count() > 0)
3534
{
36-
MaxDegreeOfParallelism = Environment.ProcessorCount
37-
};
35+
Console.WriteLine(string.Empty);
3836

39-
Parallel.ForEach(relativePaths, options, (filePath) =>
40-
{
41-
var path = Path.Combine(assetRepoRoot, filePath);
37+
var options = new ParallelOptions
38+
{
39+
MaxDegreeOfParallelism = Environment.ProcessorCount
40+
};
4241

43-
if (File.Exists(path))
42+
Parallel.ForEach(relativePaths, options, (filePath) =>
4443
{
45-
var content = File.ReadAllText(path);
46-
var fileDetections = DetectSecrets(content);
44+
var path = Path.Combine(assetRepoRoot, filePath);
4745

48-
if (fileDetections != null && fileDetections.Count > 0)
46+
if (File.Exists(path))
4947
{
50-
foreach (Detection detection in fileDetections)
48+
var content = File.ReadAllText(path);
49+
var fileDetections = DetectSecrets(content);
50+
51+
if (fileDetections != null && fileDetections.Count > 0)
5152
{
52-
detectedSecrets.Add(Tuple.Create(filePath, detection));
53+
foreach (Detection detection in fileDetections)
54+
{
55+
detectedSecrets.Add(Tuple.Create(filePath, detection));
56+
}
5357
}
54-
}
5558

56-
Interlocked.Increment(ref seen);
59+
Interlocked.Increment(ref seen);
5760

58-
Console.Write($"\r\u001b[2KScanned {seen}/{total}.");
59-
}
60-
});
61+
Console.Write($"\r\u001b[2KScanned {seen}/{total}.");
62+
}
63+
});
6164

62-
Console.WriteLine(string.Empty);
65+
Console.WriteLine(string.Empty);
66+
}
6367

6468
return detectedSecrets.ToList();
6569
}

tools/test-proxy/Azure.Sdk.Tools.TestProxy/Store/GitStore.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,19 +378,11 @@ public string[] DetectPendingChanges(GitAssetsConfiguration config)
378378

379379
if (!string.IsNullOrWhiteSpace(diffResult.StdOut))
380380
{
381-
/*
382-
* Normally, we'd use Environment.NewLine here but this doesn't work on Windows since its NewLine is \r\n and Git's NewLine is just \n
383-
*
384-
* The output from git status porcelain will include two possible additional values
385-
* " ?? path/to/file" -> File that is new
386-
* " M path/to/file" -> File that is modified
387-
* " D path/to/file" -> File that is deleted
388-
*/
381+
// Normally, we'd use Environment.NewLine here but this doesn't work on Windows since its NewLine is \r\n and
382+
// Git's NewLine is just \n
389383
var individualResults = diffResult.StdOut.Split("\n")
390-
// strim the leading space, the characters for ADDED or MODIFIED, and the space after them
391-
.Select(x => x.Trim().TrimStart('?', 'M').Trim())
392-
// exclude empty paths or paths that have been DELETED
393-
.Where(x => !string.IsNullOrWhiteSpace(x) && !x.StartsWith("D")).ToArray();
384+
.Select(x => x.Trim())
385+
.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
394386
return individualResults;
395387
}
396388

0 commit comments

Comments
 (0)