Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Merge 2c5d7b1 into a2aa0e1
Browse files Browse the repository at this point in the history
  • Loading branch information
temsa committed Aug 3, 2018
2 parents a2aa0e1 + 2c5d7b1 commit bb25066
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 324 deletions.
11 changes: 11 additions & 0 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func TestInsecureProject(t *testing.T) {
}

}

func TestNotExistingProject(t *testing.T) {
_, err := Analyze("./does-not-exist")
if err == nil {
t.Errorf("TestNotExistingProject: ./does-not-exist does not exist, it should not be analyzed !")
}
if diff := cmp.Diff(err.Error(), "stat ./does-not-exist: no such file or directory"); diff != "" {
t.Errorf("TestNotExistingProject: err : (-got +want)\n%s", diff)
}

}
149 changes: 149 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package main

import (
"log"
"os"
"path"
"testing"

"github.com/google/go-cmp/cmp"
)

var testLogFile = path.Join(os.TempDir(), "gammaray-log-test")

func cleanLogs() {
os.Remove(testLogFile)
}

func TestHelloWorld(t *testing.T) {
defer cleanLogs()
err := (&Args{
Image: "",
LogFile: testLogFile,
Path: "test_data/hello-world",
}).Run()
if err != nil {
panic(err)
}
info, error := os.Stat(testLogFile)
if error != nil {
t.Errorf("TestHelloWorld: Could not stat log file <%s>:\n%s", testLogFile, error)
}
log.Println("TestHelloWorld: log fileinfo:\n", info)

if info.Size() == 0 {
t.Errorf("TestHelloWorld: log size should not be 0!")
}

}

func TestInsecureProject(t *testing.T) {
defer cleanLogs()
err := (&Args{
Image: "",
LogFile: testLogFile,
Path: "test_data/insecure-project",
}).Run()
if err != nil {
panic(err)
}
info, error := os.Stat(testLogFile)
if error != nil {
t.Errorf("TestInsecureProject: Could not stat log file <%s>:\n%s", testLogFile, error)
}
log.Println("TestInsecureProject: log fileinfo:\n", info)

if info.Size() == 0 {
t.Errorf("TestInsecureProject: log size should not be 0!")
}

}

func TestPathDoesNotExist(t *testing.T) {
defer cleanLogs()
err := (&Args{
Image: "",
LogFile: testLogFile,
Path: "./does-not-exist",
}).Run()

if err == nil {
t.Errorf("TestPathDoesNotExist: there should be an error when trying to analyze ./does-not-exist")
}

if diff := cmp.Diff(err.Error(), "stat ./does-not-exist: no such file or directory"); diff != "" {
t.Errorf("TestPathDoesNotExist: error : (-got +want)\n%s", diff)
}
}

func TestPathIsEmpty(t *testing.T) {
defer cleanLogs()

os.Args = []string{""}

err := (&Args{
Image: "",
LogFile: testLogFile,
Path: "",
}).Run()

if err == nil {
t.Errorf("TestPathIsEmpty: there should be an error when trying to analyze an empty path/empty image")
}

if diff := cmp.Diff(err.Error(), "you need to at least properly define a path or a docker image"); diff != "" {
t.Errorf("TestPathIsEmpty: error : (-got +want)\n%s", diff)
}
}

func TestImageHelloWorld(t *testing.T) {
defer cleanLogs()
err := (&Args{
Image: "gammaray-test-hello-world:1.0.0",
LogFile: testLogFile,
Path: "",
}).Run()
if err != nil {
panic(err)
}
info, error := os.Stat(testLogFile)
if error != nil {
t.Errorf("TestImageHelloWorld: Could not stat log file <%s>:\n%s", testLogFile, error)
}
log.Println("TestImageHelloWorld: log fileinfo:\n", info)

if info.Size() == 0 {
t.Errorf("TestImageHelloWorld: log size should not be 0!")
}

}

func TestDefaults(t *testing.T) {
expected := Args{
Path: "",
Image: "",
LogFile: ".gammaray.log",
}

if diff := cmp.Diff(*Defaults(), expected); diff != "" {
t.Errorf("TestDefaults: error : (-got +want)\n%s", diff)
}
}

func TestMainHelloWorld(t *testing.T) {
defer cleanLogs()

os.Args = []string{"./gammaray", "-path", "test_data/hello-world", "-log-file", testLogFile}

main()

info, error := os.Stat(testLogFile)
if error != nil {
t.Errorf("TestMainHelloWorld: Could not stat log file <%s>:\n%s", testLogFile, error)
}
log.Println("TestMainHelloWorld: log fileinfo:\n", info)

if info.Size() == 0 {
t.Errorf("TestMainHelloWorld: log size should not be 0!")
}
}
7 changes: 7 additions & 0 deletions pathrunner/pathrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ type NodePackage struct {
func Walk(dir string) ([]NodePackage, error) {
var packagesList []NodePackage

fileInfo, err := os.Stat(dir)
if err != nil {
return nil, err
}
if !fileInfo.IsDir() {
return nil, fmt.Errorf("<%s> is not a directory, make sure to put the proper path to your project", dir)
}
filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {

if strings.HasSuffix(path, "package.json") {
Expand Down
16 changes: 11 additions & 5 deletions pathrunner/pathrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ func TestWalkInsecureProject(t *testing.T) {
}

func TestWalkDevNull(t *testing.T) {
packages, err := Walk("/dev/null")
if err != nil {
_, err := Walk("/dev/null")
if err == nil {
panic(err)
}
log.Println("TestWalkInsecureProject: packages:\n", packages)
if diff := cmp.Diff(len(packages), 0); diff != "" {
t.Errorf("TestWalkInsecureProject: packages : (-got +want)\n%s", diff)
if diff := cmp.Diff(err.Error(), "</dev/null> is not a directory, make sure to put the proper path to your project"); diff != "" {
t.Errorf("TestWalkDevNull: err : (-got +want)\n%s", diff)
}
}

func TestWalkDoesNotExist(t *testing.T) {
_, err := Walk("./does-not-exist")
if err == nil {
t.Errorf("TestWalkDoesNotExist: given ./does-not-exist does not exist, it should Error !")
}
}

0 comments on commit bb25066

Please sign in to comment.