1

I have a @Test method written in java which calls a shell script. The shell script contains vimdiff command used for generating code comparison between two html files.

When I run this test method from jenkins, the shell script is getting executed. But the vimdiff command is not getting executed.

Java method which calls shell script

try {
            File[] uiDiffDir = getFiles();

            for (File file : uiDiffDir) {

                String[] cmd = {"sh", shellScriptPath, beforeHtmlPath + file.getName(), afterHtmlPath + file.getName(),
                        codeComparisonPath + file.getName()};
                Process p = Runtime.getRuntime().exec(cmd);
                p.waitFor();
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

shell.sh

 vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'

This method is working perfectly from my intellij but from inside jenkins vimdiff is not working. From jenkins, I verified the arguments are getting passed correctly using echo statements. So thats not an issue. So my question is, does vimdiff work from inside jenkins? Can someone help me with this question. I am little confused.

Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17

2 Answers2

0

It's probably not on the $PATH for the jenkins user. Try an absolute path to vimdiff.

edit: Given your information from the comments, you probably need to

  • Create a symlink for vimdiff on your jenkins server like so:

    ln -s /usr/bin/vim.tiny /usr/bin/vimdiff 
    
  • Call that symlinked binary (/usr/bin/vimdiff) using an absolute path from your Java code

OhleC
  • 2,821
  • 16
  • 29
  • Awesome point man. Will try it out right away and update the post!! Thanks for the suggestion – Kishore Mohanavelu Sep 21 '18 at 11:50
  • I executed ' whereis vimdiff ' command in jenkins terminal and got the output ' vimdiff: /usr/share/man/man1/vimdiff.1.gz '. So I added this output to the Path variable in jenkins. Am i doing it right? Because this is my first time playing around with Jenkins – Kishore Mohanavelu Sep 21 '18 at 12:12
  • My suggestion would be to just use the absolute path in your java code. Also, the path you pasted is for the manpage; the path to the executable should also be printed by whereis and should be something like `/usr/bin/vimdiff` – OhleC Sep 21 '18 at 12:21
  • I tried ' whereis vim ' and ' whereis vimdiff '. Both are printing the following outputs. **master>whereis vim vim: /usr/bin/vim.tiny /etc/vim /usr/bin/X11/vim.tiny /usr/share/vim /usr/share/man/man1/vim.1.gz**. **master>whereis vimdiff vimdiff: /usr/share/man/man1/vimdiff.1.gz**I am not getting the path what you have posted in your comment. – Kishore Mohanavelu Sep 21 '18 at 12:35
  • So you have vim.tiny installed. I think that should be able to function as vimdiff. So you could just symlink `/usr/bin/vimdiff` to `/usr/bin/vim.tiny` (vim will go into diff mode if called as `vimdiff`) – OhleC Sep 21 '18 at 12:39
  • I added the path variable to the shell script before the vimdiff command and tried to run the script. But it did not workout. **export Path=/usr/bin/vim.tiny vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'** – Kishore Mohanavelu Sep 21 '18 at 13:04
  • That won't work for several reasons (firstly, it's `PATH`, not `Path`; also, you're setting `PATH` to a file, not a directory. Thirdly, there's still no file called `vimdiff` on that path). I've edited my answer. – OhleC Sep 21 '18 at 13:12
  • I am sorry sir. I don't know how to create symlink and how to use it in java. Can you point me to a thread which shows how to do these things? – Kishore Mohanavelu Sep 21 '18 at 15:01
  • You do that by executing the `ln` command from my answer on the Jenkins server and using the absolute path from the second bullet point instead of `vimdiff` in your original Java code. – OhleC Sep 21 '18 at 15:06
  • I executed the symlink in jenkins server and updated the shell script command as follows **/usr/bin/vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'**. Still the vimdiff command is not working – Kishore Mohanavelu Sep 21 '18 at 15:56
  • After three days of effort I found the root cause of the issue and solution to the above problem. Updating the solution below, if someone else comes across the same problem in the future – Kishore Mohanavelu Sep 24 '18 at 04:11
0

By default vim.tiny was installed in the jenkins server. vim.tiny is the compact version of vim and it did not support vimdiff as per the vim documentation. So vimdiff was not running in the shell script. As a work around, I included the following commands in the Pre-Build Steps -> Execute shell

#!/usr/bin/env bash
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe"
sudo apt-get update
sudo apt-get -y install libncurses5-dev
sudo apt remove -y vim-tiny
sudo apt-get -y install vim
echo syntax off> ~/.vimrc

Functionality of Each command:

  • sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe"

    sudo apt-get update

    sudo apt-get -y install libncurses5-dev

    The above three commands installs the necessary libraries to execute sudo apt-get install command. Providing '-y' in the sudo command will automatically take yes for the terminal prompts that asks user's permission to install

  • sudo apt remove -y vim-tiny

    sudo apt-get -y install vim

    The above two commands removes the vim-tiny and then installs the full version of vim

  • echo syntax off> ~/.vimrc

    By default syntax highlighting was ON in vimdiff. So the look and feel of the vimdiff output was very worse. So the above command creates a '.vimrc' file and adds 'syntax off' to that file and saves it. This will Turn OFF the syntax highlighting in the vimdiff output and improves the look and feel

After executing all these steps in the Jenkins Pre-Build Step, vimdiff became available inside the currently running jenkins build and I was able to use the below vimdiff command in my shell script that is called from a java method.

/usr/bin/vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'
Kishore Mohanavelu
  • 439
  • 1
  • 6
  • 17