2022-09-02 22:18:23 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-09-02 22:18:23 +03:00
package e2e
import (
"context"
"net"
"net/http"
"net/url"
2024-11-15 16:02:16 +03:00
"os"
"regexp"
2022-09-02 22:18:23 +03:00
"testing"
"time"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/tests"
2024-07-30 22:41:10 +03:00
"github.com/stretchr/testify/require"
2022-09-02 22:18:23 +03:00
)
2024-11-15 16:02:16 +03:00
var rootPathRe = regexp . MustCompile ( "\\[repository\\]\nROOT\\s=\\s.*" )
2024-08-25 15:52:21 +03:00
func onForgejoRunTB ( t testing . TB , callback func ( testing . TB , * url . URL ) , prepare ... bool ) {
2022-09-02 22:18:23 +03:00
if len ( prepare ) == 0 || prepare [ 0 ] {
defer tests . PrepareTestEnv ( t , 1 ) ( )
}
s := http . Server {
2023-08-12 19:30:16 +03:00
Handler : testE2eWebRoutes ,
2022-09-02 22:18:23 +03:00
}
u , err := url . Parse ( setting . AppURL )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2022-09-02 22:18:23 +03:00
listener , err := net . Listen ( "tcp" , u . Host )
i := 0
for err != nil && i <= 10 {
time . Sleep ( 100 * time . Millisecond )
listener , err = net . Listen ( "tcp" , u . Host )
i ++
}
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2022-09-02 22:18:23 +03:00
u . Host = listener . Addr ( ) . String ( )
2024-11-15 16:02:16 +03:00
// Override repository root in config.
conf , err := os . ReadFile ( setting . CustomConf )
require . NoError ( t , err )
require . NoError ( t , os . WriteFile ( setting . CustomConf , rootPathRe . ReplaceAll ( conf , [ ] byte ( "[repository]\nROOT = " + setting . RepoRootPath ) ) , 0 o644 ) )
2022-09-02 22:18:23 +03:00
defer func ( ) {
2024-11-15 16:02:16 +03:00
require . NoError ( t , os . WriteFile ( setting . CustomConf , conf , 0 o644 ) )
2022-09-02 22:18:23 +03:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 2 * time . Minute )
s . Shutdown ( ctx )
cancel ( )
} ( )
go s . Serve ( listener )
// Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
callback ( t , u )
}
2024-08-25 15:52:21 +03:00
func onForgejoRun ( t * testing . T , callback func ( * testing . T , * url . URL ) , prepare ... bool ) {
onForgejoRunTB ( t , func ( t testing . TB , u * url . URL ) {
2022-09-02 22:18:23 +03:00
callback ( t . ( * testing . T ) , u )
} , prepare ... )
}