Skip to content

Use Powershell for resolving paths

Arran Walker requested to merge ajwalker/powershell-path-resolve into master

What does this MR do?

Uses GetUnresolvedProviderPathFromPSPath to resolve paths.

This will eventually remove the need to use FromSlash and ToBackslash.

Why was this MR needed?

We currently resolve paths by either determining the shell used (powershell, powershell core) and use filepath functions that are specific to where Runner is hosted, not where the script is executed.

This change passes the path names to GetUnresolvedProviderPathFromPSPath, which produces an absolute path and modifies the path correctly for the platform the script is executed on.

What's the best way to test this MR?

When the FF is enabled, existing integration tests should continue to pass.

There's a unit test TestPowershellPathResolveOperations ensuring the script generated is as expected for when resolvePaths: true.

When writing this test, I originally also added the older behaviour, but it becomes quite lengthy, as the filepaths in the expected ouput changes depending on the OS the tests are run. Ultimately, I removed them, as that behaviour isn't currently being tested and hopefully this new behaviour will become the default in the near term.

A manual test is quite difficult, as it involves setting up a Runner on Linux that targets Windows. This is something I'm exploring as I add Kubernetes support for Windows.

To recreate the problem I'm trying to solve, if you add this to powershell_test.go:

func TestVariable(t *testing.T) {
	writer := &PsWriter{TemporaryPath: `C:\builds\hello`, EOL: "\n", resolvePaths: false}
	writer.Variable(common.JobVariable{File: true, Key: "a key", Value: "foobar"})
	panic(writer.String())
}

and run on Linux/MacOS, you'll see that the path used is: $CurrentDirectory\C:\builds\hello\a key. This is an invalid path because filepath.IsAbs() on Linux/MacOS doesn't understand this is an absolute Windows path.

Using resolvePaths: true, the script returned will be valid to work on Windows.

What are the relevant issue numbers?

#27712 (closed)

Edited by Arran Walker

Merge request reports