mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
windows argv to string module utility (#28970)
* windows argv to string module utility * rebased PR with latest devel branch
This commit is contained in:
parent
ed342e8ce3
commit
b663f602bc
4 changed files with 191 additions and 1 deletions
13
test/integration/targets/win_module_utils/files/PrintArgv.cs
Normal file
13
test/integration/targets/win_module_utils/files/PrintArgv.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
// This has been compiled to an exe and uploaded to S3 bucket for argv test
|
||||
|
||||
namespace PrintArgv
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(string.Join(System.Environment.NewLine, args));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
#!powershell
|
||||
|
||||
#Requires -Module Ansible.ModuleUtils.Legacy
|
||||
#Requires -Module Ansible.ModuleUtils.ArgvParser
|
||||
|
||||
$ErrorActionPreference = 'Continue'
|
||||
|
||||
$params = Parse-Args $args
|
||||
$exe = Get-AnsibleParam -obj $params -name "exe" -type "path" -failifempty $true
|
||||
|
||||
Add-Type -TypeDefinition @'
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ansible.Command
|
||||
{
|
||||
public static class NativeUtil
|
||||
{
|
||||
public static void GetProcessOutput(StreamReader stdoutStream, StreamReader stderrStream, out string stdout, out string stderr)
|
||||
{
|
||||
var sowait = new EventWaitHandle(false, EventResetMode.ManualReset);
|
||||
var sewait = new EventWaitHandle(false, EventResetMode.ManualReset);
|
||||
string so = null, se = null;
|
||||
ThreadPool.QueueUserWorkItem((s)=>
|
||||
{
|
||||
so = stdoutStream.ReadToEnd();
|
||||
sowait.Set();
|
||||
});
|
||||
ThreadPool.QueueUserWorkItem((s) =>
|
||||
{
|
||||
se = stderrStream.ReadToEnd();
|
||||
sewait.Set();
|
||||
});
|
||||
foreach(var wh in new WaitHandle[] { sowait, sewait })
|
||||
wh.WaitOne();
|
||||
stdout = so;
|
||||
stderr = se;
|
||||
}
|
||||
}
|
||||
}
|
||||
'@
|
||||
|
||||
Function Run-Process($executable, $arguments) {
|
||||
$proc = New-Object System.Diagnostics.Process
|
||||
$psi = $proc.StartInfo
|
||||
$psi.FileName = $executable
|
||||
$psi.Arguments = $arguments
|
||||
$psi.RedirectStandardOutput = $true
|
||||
$psi.RedirectStandardError = $true
|
||||
$psi.UseShellExecute = $false
|
||||
|
||||
$proc.Start() | Out-Null # will always return $true for non shell-exec cases
|
||||
$stdout = $stderr = [string] $null
|
||||
|
||||
[Ansible.Command.NativeUtil]::GetProcessOutput($proc.StandardOutput, $proc.StandardError, [ref] $stdout, [ref] $stderr) | Out-Null
|
||||
$proc.WaitForExit() | Out-Null
|
||||
$actual_args = $stdout.Substring(0, $stdout.Length - 2) -split "`r`n"
|
||||
|
||||
return $actual_args
|
||||
}
|
||||
|
||||
$tests = @(
|
||||
@('abc', 'd', 'e'),
|
||||
@('a\\b', 'de fg', 'h'),
|
||||
@('a\"b', 'c', 'd'),
|
||||
@('a\\b c', 'd', 'e'),
|
||||
@('C:\Program Files\file\', 'arg with " quote'),
|
||||
@('ADDLOCAL="a,b,c"', '/s', 'C:\\Double\\Backslash')
|
||||
)
|
||||
|
||||
foreach ($expected in $tests) {
|
||||
$joined_string = Argv-ToString -arguments $expected
|
||||
# We can't used CommandLineToArgvW to test this out as it seems to mangle
|
||||
# \, might be something to do with unicode but not sure...
|
||||
$actual = Run-Process -executable $exe -arguments $joined_string
|
||||
|
||||
if ($expected.Count -ne $actual.Count) {
|
||||
$result.actual = $actual -join "`n"
|
||||
$result.expected = $expected -join "`n"
|
||||
Fail-Json -obj $result -message "Actual arg count: $($actual.Count) != Expected arg count: $($expected.Count)"
|
||||
}
|
||||
for ($i = 0; $i -lt $expected.Count; $i++) {
|
||||
$expected_arg = $expected[$i]
|
||||
$actual_arg = $actual[$i]
|
||||
if ($expected_arg -cne $actual_arg) {
|
||||
$result.actual = $actual -join "`n"
|
||||
$result.expected = $expected -join "`n"
|
||||
Fail-Json -obj $result -message "Actual arg: '$actual_arg' != Expected arg: '$expected_arg'"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Exit-Json @{ data = 'success' }
|
|
@ -48,7 +48,7 @@
|
|||
that:
|
||||
- sid_test.data == 'success'
|
||||
|
||||
- name: create testing folder for argv binary
|
||||
- name: create temp testing folder
|
||||
win_file:
|
||||
path: C:\ansible testing
|
||||
state: directory
|
||||
|
@ -67,6 +67,15 @@
|
|||
that:
|
||||
- command_util.data == 'success'
|
||||
|
||||
- name: call module with ArgvParser tests
|
||||
argv_parser_test:
|
||||
exe: C:\ansible testing\PrintArgv.exe
|
||||
register: argv_test
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- argv_test.data == 'success'
|
||||
|
||||
- name: remove testing folder
|
||||
win_file:
|
||||
path: C:\ansible testing
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue