github.com/shravanasati/hydra@v1.0.1-0.20240122045627-1082d2ed50d2/src/init_test.go (about)

     1  /*
     2  The following code contains unittests for hydra continous integration.
     3  
     4  Author: Shravan Asati
     5  Originally Written: 21 April 2021
     6  Last Edited: 21 April 2021
     7  */
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"io/ioutil"
    14  	"os"
    15  	"testing"
    16  )
    17  
    18  func getFiles(dir string) []string {
    19  	projectFiles, er := ioutil.ReadDir(dir)
    20  	handleException(er)
    21  	projectFileNames := []string{}
    22  	for _, f := range projectFiles {
    23  		projectFileNames = append(projectFileNames, f.Name())
    24  	}
    25  	return projectFileNames
    26  }
    27  
    28  func TestPythonInit(t *testing.T) {
    29  	gwd, e := os.Getwd()
    30  	handleException(e)
    31  
    32  	// * generating random project name and license for initialisation
    33  	rlicense := generateRandom("license")
    34  	rprojectName := generateRandom("name")
    35  
    36  	init := Initializer{
    37  		projectName: rprojectName,
    38  		license:     rlicense,
    39  		lang:        "python",
    40  	}
    41  	init.pythonInit()
    42  
    43  	// * getting all files present in the directory
    44  	files, e := ioutil.ReadDir("./")
    45  	handleException(e)
    46  
    47  	// * converting into filenames
    48  	filenames := []string{}
    49  	for _, file := range files {
    50  		filenames = append(filenames, file.Name())
    51  	}
    52  
    53  	// * checking for presence
    54  	if !stringInSlice(rprojectName, filenames) {
    55  		t.Errorf("project %v not in the directory", rprojectName)
    56  	}
    57  
    58  	// * getting contents of the project initialised
    59  	projectFiles, er := ioutil.ReadDir("./")
    60  	handleException(er)
    61  	projectFileNames := []string{}
    62  	for _, f := range projectFiles {
    63  		projectFileNames = append(projectFileNames, f.Name())
    64  	}
    65  
    66  	// * checking for various files
    67  	if !stringInSlice("LICENSE", projectFileNames) {
    68  		t.Errorf("LICENSE file not present.")
    69  	}
    70  	if !stringInSlice("README.md", projectFileNames) {
    71  		t.Errorf("README.md file not present.")
    72  	}
    73  	if !stringInSlice(".gitignore", projectFileNames) {
    74  		t.Errorf(".gitignore file not present.")
    75  	}
    76  	if !stringInSlice("setup.py", projectFileNames) {
    77  		t.Errorf("setup.py file not present.")
    78  	}
    79  	if !stringInSlice(rprojectName, projectFileNames) {
    80  		t.Errorf("%v dir not present.", rprojectName)
    81  	}
    82  	if !stringInSlice("tests", projectFileNames) {
    83  		t.Errorf("tests dir not present.")
    84  	}
    85  
    86  	t.Cleanup(func() {
    87  		t.Log("Cleaning up...")
    88  		os.Chdir(gwd)
    89  		os.RemoveAll(rprojectName)
    90  	})
    91  }
    92  
    93  func TestGoInit(t *testing.T) {
    94  	gwd, e := os.Getwd()
    95  	handleException(e)
    96  
    97  	// * generating random project name and license for initialisation
    98  	rlicense := generateRandom("license")
    99  	rprojectName := generateRandom("name")
   100  	init := Initializer{
   101  		projectName: rprojectName,
   102  		license:     rlicense,
   103  		lang:        "go",
   104  	}
   105  	init.goInit()
   106  
   107  	// * getting contents of the project initialised
   108  	projectFiles, er := ioutil.ReadDir("./")
   109  	handleException(er)
   110  	projectFileNames := []string{}
   111  	for _, f := range projectFiles {
   112  		projectFileNames = append(projectFileNames, f.Name())
   113  	}
   114  
   115  	// * checking for various files
   116  	if !stringInSlice("LICENSE", projectFileNames) {
   117  		t.Errorf("LICENSE file not present.")
   118  	}
   119  	if !stringInSlice("README.md", projectFileNames) {
   120  		t.Errorf("README.md file not present.")
   121  	}
   122  	if !stringInSlice(".gitignore", projectFileNames) {
   123  		t.Errorf(".gitignore file not present.")
   124  	}
   125  	if !stringInSlice("src", projectFileNames) {
   126  		t.Errorf("src dir not present.")
   127  	}
   128  	if !stringInSlice("tests", projectFileNames) {
   129  		t.Errorf("tests dir not present.")
   130  	}
   131  
   132  	t.Cleanup(func() {
   133  		t.Log("Cleaning up...")
   134  		os.Chdir(gwd)
   135  		os.RemoveAll(rprojectName)
   136  	})
   137  }
   138  
   139  func TestWebInit(t *testing.T) {
   140  	gwd, e := os.Getwd()
   141  	handleException(e)
   142  
   143  	// * generating random project name and license for initialisation
   144  	rlicense := generateRandom("license")
   145  	rprojectName := generateRandom("name")
   146  	init := Initializer{
   147  		projectName: rprojectName,
   148  		license:     rlicense,
   149  		lang:        "web",
   150  	}
   151  	init.webInit()
   152  
   153  	// * getting contents of the project initialised
   154  	projectFileNames := getFiles("./")
   155  
   156  	// * checking for various files
   157  	fmt.Println(len(projectFileNames), projectFileNames)
   158  	if len(projectFileNames) != 8 {
   159  		t.Errorf("proper structure not made")
   160  	}
   161  
   162  	if !stringInSlice("LICENSE", projectFileNames) {
   163  		t.Errorf("LICENSE file not present.")
   164  	}
   165  	if !stringInSlice("README.md", projectFileNames) {
   166  		t.Errorf("README.md file not present.")
   167  	}
   168  	if !stringInSlice(".gitignore", projectFileNames) {
   169  		t.Errorf(".gitignore file not present.")
   170  	}
   171  	if !stringInSlice("index.html", projectFileNames) {
   172  		t.Errorf("index.html file not present.")
   173  	}
   174  	if !stringInSlice("css", projectFileNames) {
   175  		t.Errorf("css dir not present.")
   176  	}
   177  	if !stringInSlice("js", projectFileNames) {
   178  		t.Errorf("js dir not present.")
   179  	}
   180  	if !stringInSlice("img", projectFileNames) {
   181  		t.Errorf("img dir not present.")
   182  	}
   183  
   184  	cssFiles := getFiles("./css")
   185  	if !(stringInSlice("style.css", cssFiles)) {
   186  		t.Errorf("style.css not present")
   187  	}
   188  
   189  	jsFiles := getFiles("./js")
   190  	if !(stringInSlice("script.js", jsFiles)) {
   191  		t.Errorf("script.js not present")
   192  	}
   193  
   194  	t.Cleanup(func() {
   195  		t.Log("Cleaning up...")
   196  		os.Chdir(gwd)
   197  		os.RemoveAll(rprojectName)
   198  	})
   199  }