Forum Discussion

John_Laird's avatar
John_Laird
Contributor
8 years ago

odd behavior with sys.clipboard

My goal is to pipe output from command line GNU DIFF to clipboard and then log it. My problem is that Sys.Clipboard is holding the last thing I copied to clipboard BEFORE running the script. I have verified that the script is running fine when I do it by hand.

 

I have a TC12 script function that calls the GNU DIFF and then a driver function to make testing more convenient...

 

Function that does the GNU DIFF and should copy results to clipboard and then log whatever is in clipboard:

 

def diffTwoFilesToLog(file1, file2):
  diffCmd = "\"C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe\" " + file1 + " " + file2 + " | clip"
  WshShell.Run(diffCmd)
  Log.Warning(Sys.Clipboard)

Test driver for that function:

 

 

def diffTwoFilesToLogDriver():
  f1 = "C:\\imports\\exportCompare\\publishtest.txt"
  f2 = "C:\\imports\\exportCompare\\slush\\actualOCount.txt"
  diffTwoFilesToLog(f1, f2)

If I put a breakpoint on Log.Warning(Sys.Clipboard) in diffTwoFilesToLog, let it execute, go to "Locals" I see that 'diffCmd' has this value:

"C:\Program Files (x86)\GnuWin32\bin\diff.exe" C:\imports\exportCompare\publishtest.txt C:\imports\exportCompare\slush\actualOCount.txt | clip

^If I copy that exact line into my command line "dos box" and execute by hand it copies the DIFF to clipboard (desired result):

33d32
< ROC Protocol	0	{F0F0ED91-DD1E-4DE3-82AE-9B7AC011D670}

My problem is that Sys.Clipboard is instead holding the last thing I copied to clipboard BEFORE running the TC12 script.

 

Thoughts on why that is the case?

 

  • WshShell.Run by default does not wait for the command to complete, so Log.Warning(Sys.Clipboard) happens before diff completes. To make it wait you need to specify True as the third parameter of the Run method.

    WshShell.Run(diffCmd, 0, True)

     

    Alternatively, you can use Python's built-in subprocess.check_output so you won't have to deal with the clipboard:

    import subprocess

    def diffTwoFilesToLog(file1, file2):
    output = subprocess.check_output(["C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe", file1, file2])
    output = output.decode() # convert byte-string to string
    Log.Warning(output) 

6 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    WshShell.Run by default does not wait for the command to complete, so Log.Warning(Sys.Clipboard) happens before diff completes. To make it wait you need to specify True as the third parameter of the Run method.

    WshShell.Run(diffCmd, 0, True)

     

    Alternatively, you can use Python's built-in subprocess.check_output so you won't have to deal with the clipboard:

    import subprocess

    def diffTwoFilesToLog(file1, file2):
    output = subprocess.check_output(["C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe", file1, file2])
    output = output.decode() # convert byte-string to string
    Log.Warning(output) 
  • OK so I worked around the problem by not using the clipboard and getting what I needed via standard output. I changed WshShell.Run to WShShell.Exec, then got the standard output from the object that WshShel.Exec returns.

    def diffTwoFilesToLog(file1, file2):
      diffCmd = "\"C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe\" " + file1 + " " + file2
      execResult = WshShell.Exec(diffCmd)
      sout = execResult.StdOut
      Log.Warning(sout.ReadAll())

    I'm still a bit confused why Sys.Clipboard was not working before with the previous example though.

  • m_essaid's avatar
    m_essaid
    Valued Contributor

    John_Laird

    Hi,

    I'm interested about how you deal with your diff utility

    Personnaly I create a batch file which launches WinMerge with the appropriate left and right file preloaded.

    If the test reveals a difference, in my mail I write in html a link which launches the batch file.

    • John_Laird's avatar
      John_Laird
      Contributor

      m_essaid wrote:

      John_Laird

      Hi,

      I'm interested about how you deal with your diff utility

      Personnaly I create a batch file which launches WinMerge with the appropriate left and right file preloaded.

      If the test reveals a difference, in my mail I write in html a link which launches the batch file.


      The purpose of the DIFF above was to verify whether or not the software product I am testing is configured how I think it is before the test gets run (that is in an assumed state). WinMerge is a much better tool if you need a human to look at the data, in this case I do not, so I use GNU DIFF (Linux ported to Windows) which I think works better when a human does not need to intervene. See here:

       

      https://www.gnu.org/software/diffutils/manual/diffutils.html#Invoking-diff

       

      The software configuration is determined by how objects are setup in the product I am testing...so we have a dialog box that gives the user a count of how many types of each object there is. I push that object count text to file, then DIFF that against a 'test control' file and that tells me before the meat of the test gets run that the configuration is not as expected (because it can impact test results). Its one of the checks I have TC/TE run before I allow the test to continue in order to save time on the server (this particular test requires an hour to run).

       

      In another part of the same test I am recursively DIFF-ing two paired folders with a large number of files (both text and binary) in a complex directory tree. So I use these arguments instead for that:

       

      def diffSummaryTwoFolderOutputs(output1, output2, binary):
        diffCmd = "\"C:\\Program Files (x86)\\GnuWin32\\bin\\diff.exe\" " + output1 + " " + output2 + " --brief --recursive"
        if binary is True:
          diffCmd += " --binary"
        execResult = WshShell.Exec(diffCmd)
        sout = execResult.StdOut.ReadAll()
        return sout

      ^Then I put that result in a 'summary report' text file. If there are any differences found, everything gets neatly packaged into a ZIP file (>300MB of data) for later analysis by me or another human to see if the differences found are desirable or not, or just noise. WinMerge, or some other, more human friendly tool is later used for analysis.