Skip to content

Commit 7d33445

Browse files
Fix tests running (Some still does not work). #1
1 parent c7d2b14 commit 7d33445

File tree

3 files changed

+43
-50
lines changed

3 files changed

+43
-50
lines changed

ConvertApi.Cli.Test/CliTests.cs

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System.Diagnostics;
2+
using Microsoft.VisualStudio.TestPlatform.TestHost;
23

34
namespace ConvertApi.Cli.Test;
45

56
[TestFixture]
67
public class CliTests
78
{
8-
private static readonly string SolutionDirectory = GetSolutionDirectory();
9-
private static readonly string CliExecutablePath = Path.Combine(SolutionDirectory, "ConvertApi.Cli", "bin", "Debug", "net8.0", "convertapi-cli.exe");
10-
private const string ApiToken = "your_api_token_here"; // Provide your API token
11-
private static readonly string TestOutputDir = Path.Combine(SolutionDirectory, "test_output");
9+
private static readonly string CliExecutablePath = Path.Combine(Directory.GetCurrentDirectory(), "convertapi-cli.exe");
10+
private const string ApiToken = "token_YvMxGi9S"; // Provide your API token
11+
private static readonly string TestOutputDir = Path.Combine(Directory.GetCurrentDirectory(), "test_output");
1212

1313
[SetUp]
1414
public void Setup()
@@ -23,9 +23,10 @@ public void Setup()
2323
[Test]
2424
public void TestConvertPdfToDocx()
2525
{
26-
var outputFile = Path.Combine(TestOutputDir, "output.docx");
27-
var inputFile = "sample.pdf";
28-
var process = RunCli($"{ApiToken} {outputFile} {inputFile} pdf docx");
26+
var outputFile = Path.Combine(TestOutputDir, "simple.docx");
27+
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
28+
29+
var process = RunCli($"{ApiToken} {outputFile} {inputFile}");
2930

3031
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
3132
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
@@ -35,7 +36,8 @@ public void TestConvertPdfToDocx()
3536
public void TestMergePdfs()
3637
{
3738
var outputFile = Path.Combine(TestOutputDir, "merged.pdf");
38-
var process = RunCli($"{ApiToken} {outputFile} file1.pdf file2.pdf pdf merge");
39+
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
40+
var process = RunCli($"{ApiToken} {outputFile} {inputFile} {inputFile} pdf merge");
3941

4042
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
4143
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
@@ -60,49 +62,36 @@ public void TestProtectPdfWithPassword()
6062
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
6163
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
6264
}
63-
65+
6466
private Process RunCli(string arguments)
6567
{
66-
if (!File.Exists(CliExecutablePath))
67-
{
68-
throw new FileNotFoundException($"CLI executable not found at {CliExecutablePath}");
69-
}
70-
71-
var process = new Process
72-
{
73-
StartInfo = new ProcessStartInfo
74-
{
75-
FileName = CliExecutablePath,
76-
Arguments = arguments,
77-
RedirectStandardOutput = true,
78-
RedirectStandardError = true,
79-
UseShellExecute = false,
80-
CreateNoWindow = true
81-
}
82-
};
83-
84-
process.Start();
85-
process.WaitForExit();
86-
87-
Console.WriteLine(process.StandardOutput.ReadToEnd());
88-
Console.WriteLine(process.StandardError.ReadToEnd());
89-
90-
return process;
91-
}
92-
93-
private static string GetSolutionDirectory()
94-
{
95-
var directory = Directory.GetCurrentDirectory();
96-
while (!string.IsNullOrEmpty(directory) && !File.Exists(Path.Combine(directory, "ConvertApi.Cli.sln")))
97-
{
98-
directory = Directory.GetParent(directory)?.FullName;
99-
}
100-
101-
if (string.IsNullOrEmpty(directory))
102-
{
103-
throw new DirectoryNotFoundException("Solution directory not found.");
104-
}
105-
106-
return directory;
68+
if (!File.Exists(CliExecutablePath))
69+
throw new FileNotFoundException($"CLI executable not found at {CliExecutablePath}");
70+
71+
var process = new Process
72+
{
73+
StartInfo = new ProcessStartInfo
74+
{
75+
FileName = CliExecutablePath,
76+
Arguments = arguments,
77+
UseShellExecute = true, // Run in a normal console environment, because otherwise process hangs because of readline in program.cs
78+
CreateNoWindow = true
79+
}
80+
};
81+
82+
if (!process.Start())
83+
throw new InvalidOperationException("Failed to start the CLI process.");
84+
85+
// 20s to prevent infinite hangs
86+
if (!process.WaitForExit(20000))
87+
{
88+
process.Kill();
89+
throw new TimeoutException("The CLI process did not exit within 2 minutes.");
90+
}
91+
92+
if (process.ExitCode != 0)
93+
throw new Exception($"CLI process exited with code {process.ExitCode}");
94+
95+
return process;
10796
}
10897
}

ConvertApi.Cli.Test/ConvertApi.Cli.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
<Using Include="NUnit.Framework"/>
2323
</ItemGroup>
2424

25+
<ItemGroup>
26+
<ProjectReference Include="..\ConvertApi.Cli\ConvertApi.Cli.csproj" />
27+
</ItemGroup>
28+
2529
</Project>

test_files/simple.pdf

14.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)