Sunday, June 16, 2024

C# scan cs file and find variables values and names

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Xunit.Abstractions;

namespace CustomerService.Tests;

public sealed class VariableTracker
{
private readonly ITestOutputHelper _testOutputHelper;

public VariableTracker(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void ScanFile()
{
string filePath =
@"C:\Users\ozkan\projects\dotnet-examples\tc-guide-getting-started-with-testcontainers-for-dotnet\TestcontainersDemo\CustomerService\CustomerService.cs";
string code = File.ReadAllText(filePath);

SyntaxTree tree = CSharpSyntaxTree.ParseText(code);
var root = (CompilationUnitSyntax)tree.GetRoot();

var variableCollector = new VariableCollector();
variableCollector.Visit(root);

_testOutputHelper.WriteLine("\nVariable Assignments:");
foreach (var assignment in variableCollector.Assignments)
{
if (assignment.Key.ToLower().Contains("command"))
_testOutputHelper.WriteLine($"Variable: {assignment.Key}, Assigned Value: {assignment.Value}");
}
}

private class VariableCollector : CSharpSyntaxWalker
{
public Dictionary<string, string> Assignments { get; } = new();

public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
{
var left = node.Left.ToString();
var right = node.Right.ToString();
Assignments[left] = right;
base.VisitAssignmentExpression(node);
}
}
}

Saturday, June 15, 2024

C# run testcontainers and run some commands on them

 

using DotNet.Testcontainers.Builders;
using Xunit.Abstractions;
using IContainer = DotNet.Testcontainers.Containers.IContainer;

namespace TestingWithContainers;

public class CustomContainerTest(ITestOutputHelper testOutputHelper) : IAsyncLifetime
{
private IContainer[] _container;

public async Task InitializeAsync()
{
_container =
[
ContainerBuildForTest("mcr.microsoft.com/mssql/rhel/server:2019-latest"), // redhat
ContainerBuildForTest("mcr.microsoft.com/mssql/rhel/server:latest"),
ContainerBuildForTest("mcr.microsoft.com/mssql/server:2019-latest"), // ubuntu
ContainerBuildForTest("mcr.microsoft.com/mssql/server:latest"),
];

foreach (var container in _container)
{
try
{
testOutputHelper.WriteLine($"Starting container with image: {container.Image.FullName}");
await container.StartAsync();
testOutputHelper.WriteLine($"Started container with ID: {container.Id}");
}
catch (Exception ex)
{
testOutputHelper.WriteLine($"Failed to start container with image {container.Image}: {ex.Message}");
testOutputHelper.WriteLine($"Stack Trace: {ex.StackTrace}");
}
}
}

private static IContainer ContainerBuildForTest(string imageName)
{
return new ContainerBuilder()
.WithImage(imageName)
// .WithWaitStrategy(Wait.ForUnixContainer().UntilCommandIsCompleted("uname"))
.Build();
}

public async Task DisposeAsync()
{
foreach (var container in _container)
{
await container.DisposeAsync();
}
}

[Fact]
public void Run_Command()
{
Parallel.For(0, _container.Length, index =>
{
var cmd = "cat /etc/issue";
var execAsync = _container[index].ExecAsync(cmd.Split(" "), default).Result.Stdout;
testOutputHelper.WriteLine(execAsync);
// Assert.Contains("Linux", execAsync);
});
}
}

sony xperia 10 VI did not like the case

After iphone 16 I wanted to test an android and looks like sony xperia 10 VI is nice, which is 6.1 inches, but it was narrow and longer than...