github.com/jchengjr77/canaveral@v1.0.1-0.20200715160102-ea9245d1a2fb/python/addPython_test.go (about)

     1  package python
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"os/user"
     7  	"regexp"
     8  	"testing"
     9  
    10  	"github.com/jchengjr77/canaveral/lib"
    11  )
    12  
    13  func TestCreateCondaEnv(t *testing.T) {
    14  	testProjName := "testproj"
    15  	usr, err := user.Current()
    16  	lib.Check(err)
    17  	home := usr.HomeDir
    18  	workingPath := home + "/canaveral_python_ws"
    19  	if _, err = os.Stat(workingPath); os.IsNotExist(err) {
    20  		os.MkdirAll(workingPath, os.ModePerm)
    21  		defer os.RemoveAll(workingPath)
    22  	} else {
    23  		t.Errorf("canaveral folder already exists")
    24  		return
    25  	}
    26  	err = os.Chdir(workingPath)
    27  	lib.Check(err)
    28  
    29  	re, _ := regexp.Compile(testProjName)
    30  	out, err := exec.Command("conda", "info", "--envs").CombinedOutput()
    31  	if err != nil {
    32  		t.Errorf("conda info failed with %s\n", err.Error())
    33  		return
    34  	}
    35  	find := re.MatchString(string(out))
    36  	if find {
    37  		t.Errorf("bad state, conda environment already exists\n")
    38  		return
    39  	}
    40  
    41  	err = createCondaEnv(testProjName)
    42  	if err != nil {
    43  		t.Errorf("createCondaEnv failed with error: %s\n", err.Error())
    44  		return
    45  	}
    46  	re, _ = regexp.Compile(testProjName)
    47  	out, err = exec.Command("conda", "info", "--envs").CombinedOutput()
    48  	if err != nil {
    49  		t.Errorf("conda info failed with %s\n", err.Error())
    50  		return
    51  	}
    52  	find = re.MatchString(string(out))
    53  	if !find {
    54  		t.Errorf("conda failed to create environment\n")
    55  		return
    56  	}
    57  
    58  	err = exec.Command("conda", "env", "remove", "-n", testProjName).Run()
    59  	if err != nil {
    60  		t.Errorf("conda remove failed with %s\n", err.Error())
    61  		return
    62  	}
    63  }
    64  
    65  func TestActivateAndSetupConda(t *testing.T) {
    66  	testProjName := "testproj"
    67  	usr, err := user.Current()
    68  	lib.Check(err)
    69  	home := usr.HomeDir
    70  	workingPath := home + "/canaveral_python_ws"
    71  	if _, err = os.Stat(workingPath); os.IsNotExist(err) {
    72  		os.MkdirAll(workingPath, os.ModePerm)
    73  		defer os.RemoveAll(workingPath)
    74  	} else {
    75  		t.Errorf("canaveral folder already exists")
    76  		return
    77  	}
    78  	err = os.Chdir(workingPath)
    79  	lib.Check(err)
    80  
    81  	re, _ := regexp.Compile(testProjName)
    82  	out, err := exec.Command("conda", "info", "--envs").CombinedOutput()
    83  	if err != nil {
    84  		t.Errorf("conda info failed with %s\n", err.Error())
    85  		return
    86  	}
    87  	find := re.MatchString(string(out))
    88  	if find {
    89  		t.Errorf("bad state, conda environment already exists\n")
    90  		return
    91  	}
    92  
    93  	err = createCondaEnv(testProjName)
    94  	if err != nil {
    95  		t.Errorf("createCondaEnv failed with error: %s\n", err.Error())
    96  		return
    97  	}
    98  
    99  	err = activateAndSetupConda(testProjName)
   100  	if err != nil {
   101  		t.Errorf("activateAndSetupConda failed with error: %s\n", err.Error())
   102  		return
   103  	}
   104  
   105  	byteBase, err := exec.Command("conda", "info", "--base").Output()
   106  	base := string(byteBase[:len(byteBase)-1])
   107  	if !lib.FileExists(base + "/envs/" + testProjName + "/bin/python") {
   108  		t.Errorf("activateAndSetupConda failed to conda install python")
   109  		return
   110  	}
   111  	if !lib.FileExists(base + "/envs/" + testProjName + "/bin/pip") {
   112  		t.Errorf("activateAndSetupConda failed to conda install python")
   113  		return
   114  	}
   115  
   116  	err = exec.Command("conda", "env", "remove", "-n", testProjName).Run()
   117  	if err != nil {
   118  		t.Errorf("conda remove failed with %s\n", err.Error())
   119  		return
   120  	}
   121  
   122  }
   123  
   124  func TestCreateInstallSh(t *testing.T) {
   125  	usr, err := user.Current()
   126  	lib.Check(err)
   127  	home := usr.HomeDir
   128  	workingPath := home + "/canaveral_python_ws"
   129  	if _, err = os.Stat(workingPath); os.IsNotExist(err) {
   130  		os.MkdirAll(workingPath, os.ModePerm)
   131  		defer os.RemoveAll(workingPath)
   132  	} else {
   133  		t.Errorf("canaveral folder already exists")
   134  		return
   135  	}
   136  	err = os.Chdir(workingPath)
   137  	lib.Check(err)
   138  	if lib.FileExists("install_packages.sh") {
   139  		t.Errorf("Bad State, install already exists\n")
   140  		return
   141  	}
   142  	err = createInstallSh()
   143  	if err != nil {
   144  		t.Errorf("Create install failed with %s\n", err.Error())
   145  		return
   146  	}
   147  	if !lib.FileExists("install_packages.sh") {
   148  		t.Errorf("Create install packes failed to create file\n")
   149  		return
   150  	}
   151  }
   152  
   153  func TestCreateReadme(t *testing.T) {
   154  	testProjName := "testproj"
   155  	usr, err := user.Current()
   156  	lib.Check(err)
   157  	home := usr.HomeDir
   158  	workingPath := home + "/canaveral_python_ws"
   159  	if _, err = os.Stat(workingPath); os.IsNotExist(err) {
   160  		os.MkdirAll(workingPath, os.ModePerm)
   161  		defer os.RemoveAll(workingPath)
   162  	} else {
   163  		t.Errorf("canaveral folder already exists\n")
   164  		return
   165  	}
   166  	err = os.Chdir(workingPath)
   167  	lib.Check(err)
   168  	if lib.FileExists("README.md") {
   169  		t.Errorf("Bad State, readme already exists\n")
   170  		return
   171  	}
   172  	err = createREADME(testProjName, false)
   173  	if err != nil {
   174  		t.Errorf("Create readme failed with %s\n", err.Error())
   175  		return
   176  	}
   177  	if !lib.FileExists("README.md") {
   178  		t.Errorf("Create readme failed to create a readme\n")
   179  		return
   180  	}
   181  	err = os.Remove("README.md")
   182  	if err != nil {
   183  		t.Errorf("Remove readme (no conda) failed with %s\n", err.Error())
   184  		return
   185  	}
   186  	err = createREADME(testProjName, false)
   187  	if err != nil {
   188  		t.Errorf("Create readme failed with %s\n", err.Error())
   189  		return
   190  	}
   191  	if !lib.FileExists("README.md") {
   192  		t.Errorf("Create readme (conda) failed to create a readme\n")
   193  		return
   194  	}
   195  }
   196  
   197  func TestAddPythonProj(t *testing.T) {
   198  	testProjName := "testproj"
   199  	tempusr, err := user.Current()
   200  	lib.Check(err)
   201  	tempHome := tempusr.HomeDir
   202  	newPath := tempHome + "/canaveral_python_test_ws"
   203  	wsPath := tempHome + "/tmpcnavrlws_python"
   204  	f, err := os.Create(wsPath)
   205  	lib.Check(err)
   206  	defer func() {
   207  		f.Close()
   208  		os.Remove(wsPath)
   209  	}()
   210  	f.WriteString(newPath)
   211  	err = os.Chdir("../")
   212  	lib.Check(err)
   213  	dir, err := os.Getwd()
   214  	lib.Check(err)
   215  	t.Logf("\nCurrent Dir: %s\n", dir)
   216  	AddPythonProj(testProjName, wsPath)
   217  	if !lib.DirExists(newPath + "/" + testProjName) {
   218  		t.Errorf("func AddPythonProj() failed to create ws at path: %s\n",
   219  			newPath+"/"+testProjName)
   220  		return
   221  	}
   222  	if !lib.FileExists(newPath + "/" + testProjName + "/" + testProjName + ".py") {
   223  		t.Errorf("failed to create main python file\n")
   224  		return
   225  	}
   226  	if !lib.FileExists(newPath + "/" + testProjName + "/" + "install_packages.sh") {
   227  		t.Errorf("failed to create install_packages.sh file\n")
   228  		return
   229  	}
   230  	if !lib.FileExists(newPath + "/" + testProjName + "/" + "README.md") {
   231  		t.Errorf("failed to create README file\n")
   232  		return
   233  	}
   234  	re, _ := regexp.Compile(testProjName)
   235  	out, err := exec.Command("conda", "info", "--envs").CombinedOutput()
   236  	if err != nil {
   237  		t.Errorf("conda info failed with %s\n", err.Error())
   238  		return
   239  	}
   240  	find := re.MatchString(string(out))
   241  	if !find {
   242  		t.Errorf("conda failed to create environment\n")
   243  		return
   244  	}
   245  	err = exec.Command("conda", "env", "remove", "-n", testProjName).Run()
   246  	if err != nil {
   247  		t.Errorf("conda remove failed with %s\n", err.Error())
   248  		return
   249  	}
   250  	os.RemoveAll(newPath)
   251  }