0

I have a Visual Studio c# project with the following structure:

SolutionFolder
  |-Folder1
      |- Project1
      |- TestProject1
  |-Folder2
      |- Project2
      |- TestProject2
  |-TaskFolder
      |-file
 ...etc

One of the pipeline Tasks creates a folder under SolutionFolder and writes a file in it. (i.e. TaskFolder/File)

When the Testprojects are running I want to read this file. I'm calling GetCurrentDirectory in the code and in my machine this returns the Parent of the TestProject folder. i.e (Folder1) I can then go to the parent (i.e SolutionFolder) and to the file in question. TaskFolder/File

However When this runs in the Pipeline GetCurrentDirectory returns a/1 and the parent being a. so I have to add 1/s/SolutionFolder/TaskFolder/File to get to the file.

IS there a way to get a folder starting point that works for the MsAgent and the local machine. i.e (Get SolutionFolder) then /TaskFolder/File. so that the code is the same when running under each of these environments?

P.S. When running locally the File is expected to be manually place in the /TaskFolder/File as this is mainly for debugging.

Thanks in advance.

wdiaz03
  • 1
  • 1
  • As a workaround, you can write two same test methods involving read file. Hard-code the local path in testmethodA, and use the pipeline path in testmethodB. Then specify which test to run according to the test filter. About test filter, you can refer to this [case](https://stackoverflow.com/questions/63116989/how-do-i-specify-a-set-of-tests-using-vstest-in-a-pipeline/63170728#63170728). – Hugh Lin Apr 22 '21 at 09:42
  • Thank you for the reply, You idea of having two paths and selecting it as per Giulio Vian below is good ides. I will try it. – wdiaz03 May 03 '21 at 14:07

1 Answers1

0

I would check the environment variable BUILD_SOURCEDIRECTORY, if null you are in the local scenario and call GetCurrentDirectory, if not use concat SolutionFolder/TaskFolder/File to the environment variable value.

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
  • Thank you, This worth trying. – wdiaz03 May 03 '21 at 14:07
  • ``` string variable = "BUILD.SOURCESDIRECTORY"; //Build.SourcesDirectory var Envvalue = System.Environment.GetEnvironmentVariable(variable, EnvironmentVariableTarget.Machine); ``` Tried this with and without the Enviroment.VariableTarget parameter and i get Null or Empty when running on the pipeline. Any ideas? – wdiaz03 May 03 '21 at 16:32
  • careful with . (dot) vs _ (underscore) BUILD_SOURCESDIRECTORY => Environment var Build.SourcesDirectory => pipeline var in the case of C# code pick the _ (underscore) version – Giulio Vian May 04 '21 at 15:49