Skip to content

Commit e878547

Browse files
Updated tests. Run by calling cli and directly. #1
1 parent 7bc8287 commit e878547

File tree

2 files changed

+78
-40
lines changed

2 files changed

+78
-40
lines changed

ConvertApi.Cli.Test/CliTests.cs

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System.Diagnostics;
2-
using Microsoft.VisualStudio.TestPlatform.TestHost;
32

43
namespace ConvertApi.Cli.Test;
54

65
[TestFixture]
76
public class CliTests
87
{
98
private static readonly string CliExecutablePath = Path.Combine(Directory.GetCurrentDirectory(), "convertapi-cli.exe");
10-
private const string ApiToken = "token_YvMxGi9S"; // Provide your API token
9+
private const string ApiToken = "token_ST4z2qhE"; // Provide your API token
1110
private static readonly string TestOutputDir = Path.Combine(Directory.GetCurrentDirectory(), "test_output");
1211

1312
[SetUp]
@@ -21,77 +20,116 @@ public void Setup()
2120
}
2221

2322
[Test]
24-
public void TestConvertPdfToDocx()
23+
[TestCase(true)]
24+
[TestCase(false)]
25+
public void TestConvertPdfToDocx(bool cli)
2526
{
2627
var outputFile = Path.Combine(TestOutputDir, "simple.docx");
2728
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
2829

29-
var process = RunCli($"{ApiToken} {outputFile} {inputFile}");
30+
var process = Run($"{ApiToken} {outputFile} {inputFile}", cli, inputFile);
3031

31-
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
32+
if (cli)
33+
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
34+
3235
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
3336
}
3437

3538
[Test]
36-
public void TestMergePdfs()
39+
[TestCase(true)]
40+
[TestCase(false)]
41+
public void TestMergePdfs(bool cli)
3742
{
38-
var outputFile = Path.Combine(TestOutputDir, "merged.pdf");
39-
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
40-
var process = RunCli($"{ApiToken} {outputFile} {inputFile} {inputFile} pdf merge");
43+
var outputFile = Path.Combine(TestOutputDir, "simple.pdf");
44+
var inputFile1 = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
45+
var inputFile2 = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "invoice.pdf");
46+
var process = Run($"{ApiToken} {outputFile} {inputFile1} {inputFile2} pdf merge", cli, $"{inputFile1}{Environment.NewLine}{inputFile2}{Environment.NewLine}");
4147

42-
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
48+
if (cli)
49+
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
50+
4351
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
4452
}
4553

4654
[Test]
47-
public void TestAddWatermarkToPdf()
55+
[TestCase(true)]
56+
[TestCase(false)]
57+
public void TestAddWatermarkToPdf(bool cli)
4858
{
49-
var outputFile = Path.Combine(TestOutputDir, "watermarked.pdf");
50-
var process = RunCli($"{ApiToken} {outputFile} sample.pdf pdf pdf WatermarkText=Confidential");
59+
var outputFile = Path.Combine(TestOutputDir, "watermark.pdf");
60+
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
61+
var process = Run($"{ApiToken} {outputFile} {inputFile} pdf watermark Text=Confidential FileName=watermark", cli, inputFile);
5162

52-
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
63+
if (cli)
64+
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
65+
5366
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
5467
}
5568

5669
[Test]
57-
public void TestProtectPdfWithPassword()
70+
[TestCase(true)]
71+
[TestCase(false)]
72+
public void TestProtectPdfWithPassword(bool cli)
5873
{
5974
var outputFile = Path.Combine(TestOutputDir, "protected.pdf");
60-
var process = RunCli($"{ApiToken} {outputFile} sample.pdf pdf pdf UserPassword=1234 OwnerPassword=abcd");
75+
var inputFile = Path.Combine(Directory.GetCurrentDirectory(), "../../../../", "test_files", "simple.pdf");
76+
var process = Run($"{ApiToken} {outputFile} {inputFile} pdf protect UserPassword=1234 OwnerPassword=abcd FileName=protected", cli, inputFile);
6177

62-
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
78+
if (cli)
79+
Assert.AreEqual(0, process.ExitCode, "CLI did not exit cleanly.");
80+
6381
Assert.IsTrue(File.Exists(outputFile), "Output file was not created.");
6482
}
83+
84+
private Process? Run(string arguments, bool cli = true, string inputFiles = "")
85+
{
86+
if (cli)
87+
return RunCli(arguments);
88+
89+
var originalIn = Console.In;
90+
try
91+
{
92+
Console.SetIn(new StringReader(inputFiles));
93+
Program.Main(arguments.Split()).GetAwaiter().GetResult();
94+
}
95+
finally
96+
{
97+
// Restore the original input
98+
Console.SetIn(originalIn);
99+
}
100+
101+
return null;
102+
}
65103

66104
private Process RunCli(string arguments)
67105
{
68-
if (!File.Exists(CliExecutablePath))
69-
throw new FileNotFoundException($"CLI executable not found at {CliExecutablePath}");
106+
if (!File.Exists(CliExecutablePath))
107+
throw new FileNotFoundException($"CLI executable not found at {CliExecutablePath}");
70108

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-
};
109+
var process = new Process
110+
{
111+
StartInfo = new ProcessStartInfo
112+
{
113+
FileName = CliExecutablePath,
114+
Arguments = arguments,
115+
UseShellExecute = true, // Run in a normal console environment, because otherwise process hangs because of readline in program.cs
116+
CreateNoWindow = true
117+
}
118+
};
81119

82-
if (!process.Start())
83-
throw new InvalidOperationException("Failed to start the CLI process.");
120+
if (!process.Start())
121+
throw new InvalidOperationException("Failed to start the CLI process.");
84122

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-
}
123+
// 20s to prevent infinite hangs
124+
if (!process.WaitForExit(20000))
125+
{
126+
process.Kill();
127+
throw new TimeoutException("The CLI process did not exit within 2 minutes.");
128+
}
91129

92-
if (process.ExitCode != 0)
93-
throw new Exception($"CLI process exited with code {process.ExitCode}");
130+
if (process.ExitCode != 0)
131+
throw new Exception($"CLI process exited with code {process.ExitCode}");
94132

95-
return process;
133+
return process;
96134
}
97135
}

test_files/invoice.pdf

40.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)