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

feat: optimize path methods and test workflows #514

Merged
merged 39 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a847f8a
feat: optimize ExecutablePath method
devhaozi Jun 14, 2024
fb90c1e
chore: update mocks
devhaozi Jun 14, 2024
d86a9dc
feat: improve ExecutablePath
devhaozi Jun 14, 2024
7d7ed62
Merge remote-tracking branch 'origin/haozi/path' into haozi/path
devhaozi Jun 14, 2024
68710e8
chore: update mocks
devhaozi Jun 14, 2024
fdff9e3
chore: run actions
devhaozi Jun 14, 2024
785ed72
Merge remote-tracking branch 'origin/haozi/path' into haozi/path
devhaozi Jun 14, 2024
b5ccb93
fix: tests
devhaozi Jun 14, 2024
a1f2896
feat: optimize method
devhaozi Jun 14, 2024
e44aba1
feat: optimize test
devhaozi Jun 14, 2024
dc7def5
feat: optimize other path method
devhaozi Jun 14, 2024
cec6fc9
chore: update mocks
devhaozi Jun 14, 2024
e97138e
debug
devhaozi Jun 14, 2024
491e133
Merge remote-tracking branch 'origin/haozi/path' into haozi/path
devhaozi Jun 14, 2024
c3cc21c
debug
devhaozi Jun 14, 2024
0bf7cac
fix: tests
devhaozi Jun 14, 2024
6763b66
feat: update check method
devhaozi Jun 14, 2024
eccce8e
fix: tests
devhaozi Jun 14, 2024
0cc3670
fix: lint
devhaozi Jun 14, 2024
d4809a5
feat: add test case
devhaozi Jun 15, 2024
ed424d8
Merge branch 'master' into haozi/path
devhaozi Jun 16, 2024
07cf77f
Merge branch 'master' into haozi/path
devhaozi Jun 16, 2024
83a44ed
Merge branch 'master' into haozi/path
devhaozi Jun 17, 2024
438843b
fix: macOS path
devhaozi Jun 17, 2024
f6ce568
workflow: update
devhaozi Jun 17, 2024
ae0dc58
fix: tests on macos
devhaozi Jun 17, 2024
97c8f62
fix: tests on macos
devhaozi Jun 17, 2024
f430298
fix: lint
devhaozi Jun 17, 2024
23df58c
workflow: modify name
devhaozi Jun 17, 2024
51e4892
workflow: add setup docker on macOS
devhaozi Jun 17, 2024
e655ddc
workflow: modify macOS version to 13
devhaozi Jun 17, 2024
eecacc9
feat: run tests on macOS
devhaozi Jun 17, 2024
8e99068
feat: run tests on macOS
devhaozi Jun 17, 2024
8f987af
workflow: modify macOS version to 13
devhaozi Jun 17, 2024
ea9f49a
workflow: optimize docker performance
devhaozi Jun 17, 2024
db80210
workflow: revert change
devhaozi Jun 17, 2024
1dfed1c
workflow: optimize performance
devhaozi Jun 17, 2024
ac1070c
workflow: increase timeout
devhaozi Jun 17, 2024
6797fa3
workflow: remove macOS test
devhaozi Jun 17, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install dependencies
run: go mod tidy
- name: Run tests with coverage
run: go test -coverprofile="coverage.out" $(go list ./... | grep -v /mocks | grep -v /facades | grep -v /contracts | grep -v /testing/mock)
run: go test -timeout 1h -coverprofile="coverage.out" $(go list ./... | grep -v /mocks | grep -v /facades | grep -v /contracts | grep -v /testing/mock)
- name: Print coverage report
run: go tool cover -func=coverage.out
- name: Upload coverage report to Codecov
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ jobs:
with:
go-version: 'stable'
cache: false
- name: go mod pakcage cache
- name: Go mod cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.mod') }}
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod tidy
- name: Lint
uses: golangci/golangci-lint-action@v6
with:
skip-cache: true
skip-pkg-cache: true
skip-build-cache: true
version: latest
args: --timeout=30m ./...
30 changes: 19 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,49 @@ on:
- master
pull_request:
jobs:
test:
ubuntu:
strategy:
matrix:
go: ["1.21", "1.22"]
go: [ "1.21", "1.22" ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: go mod pakcage cache
- name: Go mod cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test ./...
run: go test -timeout 1h ./...
windows:
strategy:
matrix:
go: ["1.21", "1.22"]
go: [ "1.21", "1.22" ]
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: go mod pakcage cache
- name: Go mod cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}
path: |
~\AppData\Local\go-build
~\go\pkg\mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test ./...
run: go test -timeout 1h ./...
16 changes: 8 additions & 8 deletions contracts/foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ type Application interface {
// Commands register the given commands with the console application.
Commands([]console.Command)
// Path gets the path respective to "app" directory.
Path(path string) string
Path(path ...string) string
// BasePath get the base path of the Goravel installation.
BasePath(path string) string
BasePath(path ...string) string
// ConfigPath get the path to the configuration files.
ConfigPath(path string) string
ConfigPath(path ...string) string
// DatabasePath get the path to the database directory.
DatabasePath(path string) string
DatabasePath(path ...string) string
// StoragePath get the path to the storage directory.
StoragePath(path string) string
StoragePath(path ...string) string
// LangPath get the path to the language files.
LangPath(path string) string
LangPath(path ...string) string
// PublicPath get the path to the public directory.
PublicPath(path string) string
PublicPath(path ...string) string
// ExecutablePath get the path to the executable of the running Goravel application.
ExecutablePath() (string, error)
ExecutablePath(path ...string) string
// Publishes register the given paths to be published by the "vendor:publish" command.
Publishes(packageName string, paths map[string]string, groups ...string)
// CurrentLocale get the current application locale.
Expand Down
56 changes: 25 additions & 31 deletions foundation/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var _ = flag.String("env", ".env", "custom .env path")

func init() {
setEnv()
setRootPath()

app := &Application{
Container: NewContainer(),
Expand Down Expand Up @@ -58,48 +59,49 @@ func (app *Application) Boot() {
})
app.bootArtisan()
app.setTimezone()
setRootPath()
}

func (app *Application) Commands(commands []consolecontract.Command) {
app.registerCommands(commands)
}

func (app *Application) Path(path string) string {
return filepath.Join("app", path)
func (app *Application) Path(path ...string) string {
path = append([]string{"app"}, path...)
return filepath.Join(path...)
}

func (app *Application) BasePath(path string) string {
return filepath.Join("", path)
func (app *Application) BasePath(path ...string) string {
return filepath.Join(path...)
}

func (app *Application) ConfigPath(path string) string {
return filepath.Join("config", path)
func (app *Application) ConfigPath(path ...string) string {
path = append([]string{"config"}, path...)
return filepath.Join(path...)
}

func (app *Application) DatabasePath(path string) string {
return filepath.Join("database", path)
func (app *Application) DatabasePath(path ...string) string {
path = append([]string{"database"}, path...)
return filepath.Join(path...)
}

func (app *Application) StoragePath(path string) string {
return filepath.Join("storage", path)
func (app *Application) StoragePath(path ...string) string {
path = append([]string{"storage"}, path...)
return filepath.Join(path...)
}

func (app *Application) LangPath(path string) string {
return filepath.Join("lang", path)
func (app *Application) LangPath(path ...string) string {
path = append([]string{"lang"}, path...)
return filepath.Join(path...)
}

func (app *Application) PublicPath(path string) string {
return filepath.Join("public", path)
func (app *Application) PublicPath(path ...string) string {
path = append([]string{"public"}, path...)
return filepath.Join(path...)
}

func (app *Application) ExecutablePath() (string, error) {
executable, err := os.Executable()
if err != nil {
return "", err
}

return filepath.Dir(executable), nil
func (app *Application) ExecutablePath(path ...string) string {
path = append([]string{support.RootPath}, path...)
return filepath.Join(path...)
}

func (app *Application) Publishes(packageName string, paths map[string]string, groups ...string) {
Expand Down Expand Up @@ -260,15 +262,7 @@ func setEnv() {
}

func setRootPath() {
rootPath := getCurrentAbsolutePath()

// Hack the air path
airPath := "/storage/temp"
if strings.HasSuffix(rootPath, airPath) {
rootPath = strings.ReplaceAll(rootPath, airPath, "")
}

support.RootPath = rootPath
support.RootPath = getCurrentAbsolutePath()
}

func getEnvPath() string {
Expand Down
13 changes: 12 additions & 1 deletion foundation/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package foundation

import (
"log"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -83,14 +84,24 @@ func (s *ApplicationTestSuite) TestStoragePath() {
s.Equal(filepath.Join("storage", "goravel.go"), s.app.StoragePath("goravel.go"))
}

func (s *ApplicationTestSuite) TestLangPath() {
s.Equal(filepath.Join("lang", "goravel.go"), s.app.LangPath("goravel.go"))
}

func (s *ApplicationTestSuite) TestPublicPath() {
s.Equal(filepath.Join("public", "goravel.go"), s.app.PublicPath("goravel.go"))
}

func (s *ApplicationTestSuite) TestExecutablePath() {
executable, err := s.app.ExecutablePath()
path, err := os.Getwd()
s.NoError(err)

executable := s.app.ExecutablePath()
s.NotEmpty(executable)
executable2 := s.app.ExecutablePath("test")
s.Equal(filepath.Join(path, "test"), executable2)
executable3 := s.app.ExecutablePath("test", "test2/test3")
s.Equal(filepath.Join(path, "test", "test2/test3"), executable3)
}

func (s *ApplicationTestSuite) TestPublishes() {
Expand Down
49 changes: 29 additions & 20 deletions foundation/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,48 @@
"log"
"os"
"path/filepath"
"runtime"
"strings"
)

func getCurrentAbsolutePath() string {
dir := getCurrentAbsolutePathByExecutable()
tmpDir, _ := filepath.EvalSymlinks(os.TempDir())
if strings.Contains(dir, tmpDir) {
return getCurrentAbsolutePathByCaller()
executable, err := os.Executable()
if err != nil {
log.Fatal(err)

Check warning on line 13 in foundation/path.go

View check run for this annotation

Codecov / codecov/patch

foundation/path.go#L13

Added line #L13 was not covered by tests
}
res, _ := filepath.EvalSymlinks(filepath.Dir(executable))

if isTesting() || isAir() || isDirectlyRun() {
res, _ = os.Getwd()
}

return dir
return res
}

func getCurrentAbsolutePathByExecutable() string {
exePath, err := os.Executable()
if err != nil {
log.Fatal(err)
// isTesting checks if the application is running in testing mode.
func isTesting() bool {
for _, arg := range os.Args {
if strings.Contains(arg, "-test.") {
return true
}
}
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))

return res
return false

Check warning on line 32 in foundation/path.go

View check run for this annotation

Codecov / codecov/patch

foundation/path.go#L32

Added line #L32 was not covered by tests
}

func getCurrentAbsolutePathByCaller() string {
var abPath string
for i := 0; i < 15; i++ {
_, filename, _, ok := runtime.Caller(i)
if ok && strings.HasSuffix(filename, "main.go") {
abPath = filepath.Dir(filename)
break
// isAir checks if the application is running using Air.
func isAir() bool {
for _, arg := range os.Args {
if strings.Contains(arg, "/storage/temp") {
return true

Check warning on line 39 in foundation/path.go

View check run for this annotation

Codecov / codecov/patch

foundation/path.go#L36-L39

Added lines #L36 - L39 were not covered by tests
}
}

return abPath
return false

Check warning on line 43 in foundation/path.go

View check run for this annotation

Codecov / codecov/patch

foundation/path.go#L43

Added line #L43 was not covered by tests
}

// isDirectlyRun checks if the application is running using go run.
func isDirectlyRun() bool {
executable, _ := os.Executable()
return strings.Contains(filepath.Base(executable), os.TempDir()) ||
(strings.Contains(executable, "/var/folders") && strings.Contains(executable, "/T/go-build")) // macOS

Check warning on line 50 in foundation/path.go

View check run for this annotation

Codecov / codecov/patch

foundation/path.go#L47-L50

Added lines #L47 - L50 were not covered by tests
}
Loading
Loading