Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e and integration tests #32

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ node_modules/
dist/
first.html
second.html
**/.DS_Store
**/.DS_Store
test/temp/
test/cypress/screenshots/*
test/cypress/videos/*
!test/resources/**/node_modules/
27 changes: 27 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const {defineConfig} = require("cypress");
const {prepareTestEnvironment, cleanup} = require("./test/common");
const path = require("path");

module.exports = defineConfig({
e2e: {
setupNodeEvents(on) {
on('task', {
prepareEnv(reportParams) {
const preparingPromise = prepareTestEnvironment(reportParams)();
return preparingPromise.then(() => {
return path.join('test', 'temp', reportParams.outputFileName);
})
},
cleanup() {
cleanup();
return null;
},
})
},
downloadsFolder: 'test/cypress/downloads',
screenshotsFolder: 'test/cypress/screenshots',
specPattern: 'test/cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
supportFile: 'test/cypress/support/e2e.{js,jsx,ts,tsx}',
videosFolder: 'test/cypress/videos',
},
});
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ module github.com/afdesk/scan2html

go 1.19

require golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect
require (
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 28 additions & 13 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,43 @@ const gulp = require("gulp");
const cleanCSS = require("gulp-clean-css");
const inline = require("gulp-inline");
const uglify = require("gulp-uglify");
const htmlSplit = require('gulp-htmlsplit');
const htmlSplit = require("gulp-htmlsplit");
const argv = require('yargs').argv;

function makeInline() {
return gulp
.src("./src/index.html")
.pipe(
inline({
base: "./src",
js: uglify,
css: function () {
return cleanCSS();
},
})
);
return gulp.src("./src/index.html").pipe(
inline({
base: "./src",
js: uglify,
css: function () {
return cleanCSS();
},
})
);
}

gulp.task('default', function () {
let dist;
if (argv.dist) {
dist = argv.dist;
} else {
dist = './dist'
}
return makeInline()
.pipe(htmlSplit())
.pipe(gulp.dest("./dist"));
.pipe(gulp.dest(dist));
})

gulp.task("dev", function () {
return gulp
.src("./src/index.html")
.pipe(htmlSplit())
.pipe(gulp.dest("./dist"));
});

gulp.task("single", function () {
return makeInline().pipe(gulp.dest("./dist"));
});

gulp.task('single', function () {
return makeInline()
Expand Down
19 changes: 19 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build mage
// +build mage

package main

import (
"log"
"os"
"os/exec"
)

func Test() {
cmd := exec.Command("go", "test")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failer to run tests: %v", err)
}
}
33 changes: 20 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"golang.org/x/xerrors"

"golang.org/x/exp/slices"
)

Expand All @@ -18,32 +20,42 @@ var (
)

func main() {
if err := run(); err != nil {
log.Fatalf("failed to read html file: %v", err)
}
}

func run() (err error) {
if slices.Contains(os.Args, "-h") || slices.Contains(os.Args, "--help") {
helpMessage()
}
jsonResultFile := getFlagValue("--load-result")
if jsonResultFile == "" {
jsonResultFile = filepath.Join(os.TempDir(), tempJsonFileName)
defer removeFile(jsonResultFile)
err := makeTrivyJsonReport(jsonResultFile)
defer func(file string) {
if removeErr := os.Remove(file); removeErr != nil {
err = xerrors.Errorf("failed to remove file: %w", removeErr)
}
}(jsonResultFile)
err = makeTrivyJsonReport(jsonResultFile)
if err != nil {
log.Fatalf("failed to build report: %v", err)
return xerrors.Errorf("failed to build report: %w", err)
}
}

firstHTML, err := readPluginFile("first.html")
if err != nil {
log.Fatalf("failed to read html file: %v", err)
return xerrors.Errorf("failed to read html file: %w", err)
}

reportJson, err := os.ReadFile(jsonResultFile)
if err != nil {
log.Fatalf("failed to read json file: %v", err)
return xerrors.Errorf("failed to read json file: %w", err)
}

secondHTML, err := readPluginFile("second.html")
if err != nil {
log.Fatalf("failed to read html file: %v", err)
return xerrors.Errorf("failed to read html file: %w", err)
}

createdAt := time.Now().Unix()
Expand All @@ -53,14 +65,9 @@ func main() {

err = os.WriteFile(os.Args[len(os.Args)-1], append(firstHTML, output...), 0600)
if err != nil {
log.Fatalf("failed to write output file: %v", err)
}
}

func removeFile(file string) {
if err := os.Remove(file); err != nil {
log.Fatalf("failed to remove file %v", err)
return xerrors.Errorf("failed to write output file: %w", err)
}
return nil
}

func helpMessage() {
Expand Down
82 changes: 82 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"github.com/stretchr/testify/assert"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"testing"
)

var cwd = ""

func initializeCwd() {
currentWorkingDirectory, err := os.Getwd()
if err != nil {
log.Fatalf("failed to get working directory: %v", err)
}
cwd = currentWorkingDirectory
}

func buildHtmlFiles() {
ex, err := os.Executable()
if err != nil {
log.Fatalf("failed to get executable, %v", err)
}
cmd := exec.Command("yarn", "build", "--dist", filepath.Dir(ex))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failer to build html files: %v", err)
}
}

func TestMain(m *testing.M) {
initializeCwd()
buildHtmlFiles()
os.Exit(m.Run())
}

func Test_main(t *testing.T) {
vulnSourcePath := path.Join(cwd, "resources", "vulnSource")
defer func() { os.Args = []string{} }()
tests := []struct {
name string
osArgs []string
tempFilePath string
}{{
name: "test image",
osArgs: []string{"scan2html", "image", "alpine:3.14.1"},
tempFilePath: path.Join(os.TempDir(), "scan2html_image_test.html"),
}, {
name: "test fs",
osArgs: []string{"scan2html", "fs", vulnSourcePath},
tempFilePath: path.Join(os.TempDir(), "scan2html_fs_test.html"),
}, {
name: "test rootfs",
osArgs: []string{"scan2html", "rootfs", vulnSourcePath},
tempFilePath: path.Join(os.TempDir(), "scan2html_rootfs_test.html"),
},
}
defer func(tests []struct {
name string
osArgs []string
tempFilePath string
}) {
for _, test := range tests {
err := os.Remove(test.tempFilePath)
if err != nil {
log.Fatalf("failded to delete temp files: %v", err)
}
}
}(tests)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = append(tt.osArgs, tt.tempFilePath)
assert.Nil(t, run())
})
}
}
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
{
"scripts": {
"build": "gulp",
"build-single": "gulp single"
"build-dev": "gulp dev",
"build-single": "gulp single",
"test:unit": "mocha test/unit/test.js --timeout 20000",
"test:e2e": "cypress run",
"test:e2e:open": "cypress open"
},
"engines": {
"node": ">=12.0.0"
},
"devDependencies": {
"chai": "^4.3.7",
"cypress": "^12.7.0",
"gulp": "^4.0.2",
"gulp-clean-css": "^4.3.0",
"gulp-cli": "^2.3.0",
"gulp-htmlsplit": "^1.1.1",
"gulp-inline": "^0.1.3",
"gulp-uglify": "^3.0.2",
"prettier": "^2.8.4"
"jsdom": "^21.1.1"
}
}
Loading