简体   繁体   中英

Exe Process only works in VS2010 unit tests, not from MVC app

I have a class which handles execution of an exe process, which is a console application (written in Fortran!). When running this program, the user must first enter the name of an input file (which must exist in the same location as the exe), and an output file (which does not yet exist, but is produced by the exe.

In my unit tests for the Execute method (see below), everything works fine. The output file is produced correctly. However when I hit this method from my MVC application, the exe fails to find the input file, even though it does exist. The only explanation I can think of for this is that the "TestResults" folders automatically created by VS2010 are different somehow in terms of permissions, or that the unit testing process is different somehow.

    public void Execute(string inputFileName, string outputFileName, int timeoutMilliseconds)
    {
        if (outputFileName == null)
            throw new ArgumentNullException("outputFileName");
        else if (inputFileName == null)
            throw new ArgumentNullException("inputFileName");

        if (outputFileName == string.Empty)
            throw new ArgumentException("outputFileName must not be an empty string");
        else if (inputFileName == string.Empty)
            throw new ArgumentException("inputFileName must not be an empty string");

        if (!File.Exists(ConfigurationManager.AppSettings["dataPath"]  + inputFileName))
            throw new FileNotFoundException("File not found.", inputFileName);            

        Process p = new Process();
        p.StartInfo = GetProcessStartInfo();
        p.Start();

        string output = "";

        // Read up to line where program asks for input file name.
        while (output == "")            
            output = p.StandardOutput.ReadLine();            

        // Write the input file name.
        p.StandardInput.WriteLine(inputFileName);

        // Read up to line where program asks for output file name.
        output = "";
        while (output == "")            
            output = p.StandardOutput.ReadLine();            

        // Write the output file name.
        p.StandardInput.WriteLine(outputFileName);

        if(!p.WaitForExit(timeoutMilliseconds))
            throw new ApplicationException("The process timed out waiting for a response.");            
    }

    private ProcessStartInfo GetProcessStartInfo()
    {
        var startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = true;
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardInput = true;
        startInfo.FileName = _exeFileLocation;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;            
        return startInfo;
    }

EDIT:

I have discovered that the unit test also fails if I try to run the process from a location on my C drive. In the code below if I set "dataPath" to a hard disk location (eg "C:\\DATALOCATION"), the process fails saying it can't locate the input file. However if I leave "dataPath" in the config to an empty string, the unit test automatically runs the process from a subdirectory of the TestResults folder, and all works!

    [TestMethod]
    public void ExeRunnerTest()
    {
        var studyData = // populate study data here
        var writer = new StudyDataFileWriter(studyData);

        // Write input
        string fileName = writer.WriteToFile();

        // Run 
        var exeRunner = new ExeRunner(ConfigurationManager.AppSettings["dataPath"] + "myProcess.exe");
        exeRunner.Execute(fileName, "outputFile", 10000);

        // Read output
        IStudyOutputFileReader reader = new StudyOutputFileReader(ConfigurationManager.AppSettings["dataPath"] + "outputFile");
        StudyOutput output = reader.Read();

        // Tests
        Assert.AreEqual(1, output.ConsumerSummary.ConsumerTypes.Count());
    }

Using absolute paths got the code working. There must have been an issue with the working directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM