2020-05-27 18:55:44 +07:00
|
|
|
package suites
|
|
|
|
|
2020-11-28 07:06:42 +07:00
|
|
|
import (
|
2021-11-05 20:14:42 +07:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-10-08 07:17:08 +07:00
|
|
|
"io/ioutil"
|
2021-11-05 20:14:42 +07:00
|
|
|
"log"
|
2020-11-28 07:06:42 +07:00
|
|
|
"os"
|
2021-10-08 07:17:08 +07:00
|
|
|
"path/filepath"
|
2021-11-05 20:14:42 +07:00
|
|
|
"runtime"
|
2021-10-08 07:17:08 +07:00
|
|
|
"strings"
|
2021-11-05 20:14:42 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-rod/rod"
|
2020-11-28 07:06:42 +07:00
|
|
|
)
|
|
|
|
|
2020-05-27 18:55:44 +07:00
|
|
|
// GetLoginBaseURL returns the URL of the login portal and the path prefix if specified.
|
|
|
|
func GetLoginBaseURL() string {
|
|
|
|
if PathPrefix != "" {
|
|
|
|
return LoginBaseURL + PathPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return LoginBaseURL
|
|
|
|
}
|
2020-11-28 07:06:42 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
func (rs *RodSession) collectCoverage(page *rod.Page) {
|
|
|
|
coverageDir := "../../web/.nyc_output"
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
resp, err := page.Eval("JSON.stringify(window.__coverage__)")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2020-11-28 07:06:42 +07:00
|
|
|
}
|
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
coverageData := fmt.Sprintf("%v", resp.Value)
|
2020-11-28 07:06:42 +07:00
|
|
|
|
2021-11-05 20:14:42 +07:00
|
|
|
_ = os.MkdirAll(coverageDir, 0775)
|
|
|
|
|
|
|
|
if coverageData != "<nil>" {
|
|
|
|
err = ioutil.WriteFile(fmt.Sprintf("%s/coverage-%d.json", coverageDir, now.Unix()), []byte(coverageData), 0664) //nolint:gosec
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = filepath.Walk("../../web/.nyc_output", fixCoveragePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *RodSession) collectScreenshot(err error, page *rod.Page) {
|
2021-12-01 10:32:58 +07:00
|
|
|
if err == context.DeadlineExceeded && os.Getenv("CI") == t {
|
2021-11-05 20:14:42 +07:00
|
|
|
base := "/buildkite/screenshots"
|
|
|
|
build := os.Getenv("BUILDKITE_BUILD_NUMBER")
|
|
|
|
suite := strings.ToLower(os.Getenv("SUITE"))
|
|
|
|
job := os.Getenv("BUILDKITE_JOB_ID")
|
|
|
|
path := filepath.Join(fmt.Sprintf("%s/%s/%s/%s", base, build, suite, job)) //nolint: gocritic
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc, _, _, _ := runtime.Caller(2)
|
|
|
|
fn := runtime.FuncForPC(pc)
|
|
|
|
p := "github.com/authelia/authelia/v4/internal/suites."
|
|
|
|
r := strings.NewReplacer(p, "", "(", "", ")", "", "*", "", ".", "-")
|
|
|
|
|
|
|
|
page.MustScreenshotFullPage(fmt.Sprintf("%s/%s.jpg", path, r.Replace(fn.Name())))
|
|
|
|
}
|
2020-11-28 07:06:42 +07:00
|
|
|
}
|
2021-10-08 07:17:08 +07:00
|
|
|
|
|
|
|
func fixCoveragePath(path string, file os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
coverage, err := filepath.Match("*.json", file.Name())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if coverage {
|
|
|
|
read, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
ciPath := strings.TrimSuffix(wd, "internal/suites")
|
|
|
|
content := strings.ReplaceAll(string(read), "/node/src/app/", ciPath+"web/")
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(path, []byte(content), 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|