Skip to content

Commit

Permalink
Merge pull request #48 from zong-zhe/more-e2e-test
Browse files Browse the repository at this point in the history
feat: add more e2e test
  • Loading branch information
Peefy authored Feb 29, 2024
2 parents bdafbaa + e560b42 commit 9f16256
Show file tree
Hide file tree
Showing 147 changed files with 1,093 additions and 36 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: e2e test
on: [push, pull_request]

jobs:
e2e:
name: e2e test
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: install Go
uses: actions/setup-go@v2
with:
go-version: 1.21

- name: run e2e
run: |
make e2e
4 changes: 2 additions & 2 deletions cmd/kcl/commands/mod_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewModInitCmd(cli *client.KpmClient) *cobra.Command {
pwd, err := os.Getwd()

if err != nil {
reporter.Fatal("kpm: internal bugs, please contact us to fix it")
reporter.Fatal("internal bugs, please contact us to fix it")
}

var pkgName string
Expand Down Expand Up @@ -83,7 +83,7 @@ func NewModInitCmd(cli *client.KpmClient) *cobra.Command {
return err
}

reporter.ReportMsgTo(fmt.Sprintf("kpm: package '%s' init finished", pkgName), cli.GetLogWriter())
reporter.ReportMsgTo(fmt.Sprintf("package '%s' init finished", pkgName), cli.GetLogWriter())
return nil
},
SilenceUsage: true,
Expand Down
4 changes: 2 additions & 2 deletions cmd/kcl/commands/mod_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func NewModPkgCmd(cli *client.KpmClient) *cobra.Command {
pwd, err := os.Getwd()

if err != nil {
reporter.ExitWithReport("kpm: internal bug: failed to load working directory")
reporter.ExitWithReport("internal bug: failed to load working directory")
}

kclPkg, err := pkg.LoadKclPkg(pwd)

if err != nil {
reporter.ExitWithReport("kpm: failed to load package in " + pwd + ".")
reporter.ExitWithReport("failed to load package in " + pwd + ".")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kcl/commands/mod_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func pushPackage(ociUrl string, kclPkg *pkg.KclPkg, vendorMode bool, cli *client
return err
}

reporter.ReportMsgTo(fmt.Sprintf("kpm: package '%s' will be pushed", kclPkg.GetPkgName()), cli.GetLogWriter())
reporter.ReportMsgTo(fmt.Sprintf("package '%s' will be pushed", kclPkg.GetPkgName()), cli.GetLogWriter())
// 4. Push it.
err = cli.PushToOci(tarPath, ociOpts)
if err != (*reporter.KpmEvent)(nil) {
Expand Down
136 changes: 136 additions & 0 deletions docs/dev-guide-e2e.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

# Add e2e test

## 1. Quick Start
We have prepared the `e2e-init` command to help you initialize an empty e2e test case. The command format is as follows:

```
make e2e-init TS=<test suite name>
```

`<test suite name>` is the name of the test case.

For example, to add a test case named `test_kcl_mod_init_1` to test the output of the `kcl mod init` command in a KCL package directory, you can use the following command:

### 1.1. Create an empty test case
```
make e2e-init TS=test_kcl_mod_init
```

You will get the following result:

```
Test suite created successfully in ./test/e2e/test_suites/test_kcl_mod_init_1.
```
By this command, we can see that a new test case `test_kcl_mod_init_1` has been created in the `test/e2e/test_suites` directory of the project.

```
$ tree test/e2e/test_suites/test_kcl_mod_init_1
test_kcl_mod_init_1
├── input # The input of the test case
├── stderr # The expected output in stderr
├── stdout # The expected output in stdout
└── test_space # The test space is a empty directory and the `input` will run in this directory
```

### 1.2. Add the test environment to the `test_space` directory.

For `test_kcl_mod_init_1`, we need to create a KCL package directory in the `test_space` directory. The directory structure is as follows:

```
$tree test/e2e/test_suites/test_kcl_mod_init_1/test_space
test_space
├── kcl.mod
├── kcl.mod.lock
└── main.k
```

### 1.3. Add the command to be tested to the `input` file.

The content of the `input` file is as follows:

```
kcl mod init
```

### 1.4. The expected output

`stderr` is empty, and the content in `stdout` is as follows:

```
creating new :<workspace>/test_space/kcl.mod
'<workspace>/test_space/kcl.mod' already exists
creating new :<workspace>/test_space/kcl.mod.lock
'<workspace>/test_space/kcl.mod.lock' already exists
creating new :<workspace>/main.k
'<workspace>/main.k' already exists
package 'test_space' init finished
```

In the content of the `stdout` file, `<workspace>` is a variable that will be replaced with the absolute path of the `test_space` in the test process.

### 1.5. Start test

After completing these steps, run `make e2e` to start the test process.

The test results will be displayed as follows:

```
Ran 3 of 3 Specs in 0.102 seconds
SUCCESS! -- 3 Passed | 0 Failed | 0 Pending | 0 Skipped
```

## 2. Adjust the execution path of the tested command

By default, the command in the `input` file is executed in the `test_space` directory. However, in some more complex cases, we may want to execute the command in a subdirectory of `test_space`. We can use the `conf.json` file to specify the directory in which the command is executed.

Take `test_kcl_mod_add_local` as an example to show the process. In this test case, we want to test the process of adding a KCL package in the local directory as a third-party dependency through the `kcl mod add` command.

### 2.1. Use `e2e-init` to create a test case

```
make e2e-init TS=test_kcl_mod_add_local
```

### 2.2. Prepare the test environment

In the `test_space` directory, prepare two KCL packages. The `pkg` package will add the `dep` package as a dependency through the `kcl mod add` command.

```
test_space
├── dep
│ ├── kcl.mod
│ ├── kcl.mod.lock
│ └── main.k
└── pkg
├── kcl.mod
├── kcl.mod.lock
└── main.k
```

### 2.3. Configure the execution directory

Add a `conf.json` file to the `test_kcl_mod_add_local` directory to specify the execution directory of the command.

```
{
"cwd": “test_space/pkg"
}
```

### 2.4. Add the command to be tested to the `input` file

The content of the `input` file is as follows:

```
kcl mod add <workspace>/dep
```

### 2.5. Add the expected output

Add the expected output to the `stdout` file.

```
adding dependency 'dep'
add dependency 'dep:0.0.1' successfully
```
36 changes: 25 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ go 1.21

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
github.com/spf13/cobra v1.8.0
kcl-lang.io/kcl-go v0.8.0-beta.1
kcl-lang.io/kcl-openapi v0.5.5
kcl-lang.io/kcl-playground v0.5.1
kcl-lang.io/kpm v0.8.0-beta.1
)

require google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.3.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
)

require (
dario.cat/mergo v1.0.0 // indirect
Expand Down Expand Up @@ -40,10 +54,10 @@ require (
github.com/docker/go-units v0.5.0 // indirect
github.com/dominikbraun/graph v0.23.0
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/fatih/color v1.10.0
github.com/getkin/kin-openapi v0.123.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/gin v1.7.7 // indirect
github.com/gin-gonic/gin v1.9.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/go-git/go-git/v5 v5.11.0 // indirect
Expand All @@ -59,9 +73,9 @@ require (
github.com/go-openapi/strfmt v0.21.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/go-openapi/validate v0.21.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
Expand All @@ -86,10 +100,10 @@ require (
github.com/klauspost/compress v1.16.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/locker v1.0.1 // indirect
Expand All @@ -101,7 +115,7 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/otiai10/copy v1.9.0 // indirect
github.com/otiai10/copy v1.9.0
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1
Expand All @@ -116,8 +130,8 @@ require (
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/pflag v1.0.5
github.com/thoas/go-funk v0.9.3 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/thoas/go-funk v0.9.3
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/yuin/goldmark v1.7.0 // indirect
Expand Down
Loading

0 comments on commit 9f16256

Please sign in to comment.