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

Moon bit test agent #1053

Open
wants to merge 101 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
7396fb3
workflow for MoonBit Test Agent
KirbytroNic0528 Sep 30, 2024
557f851
MoonBit Test Agent Code
KirbytroNic0528 Sep 30, 2024
7518a5c
Merge branch 'main' into MoonBit-Test-Agent
tonyfettes Oct 8, 2024
7ddda72
Merge branch 'main' into MoonBit-Test-Agent
tonyfettes Oct 8, 2024
ce0bacc
Update gettest.py
KirbytroNic0528 Oct 9, 2024
25c1f82
Update readcoverage.py
KirbytroNic0528 Oct 9, 2024
8ac8123
Update requirement.txt
KirbytroNic0528 Oct 9, 2024
7ce6b71
Update testagent.py
KirbytroNic0528 Oct 9, 2024
3d4b2af
Update writedown.py
KirbytroNic0528 Oct 9, 2024
fb1510c
rename requirement.txt to requirements.txt
KirbytroNic0528 Oct 9, 2024
8fd3751
Update requirements.txt
KirbytroNic0528 Oct 10, 2024
2022af8
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
2d4f6ef
Update requirements.txt
KirbytroNic0528 Oct 11, 2024
7f340c8
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
d01f5d1
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
6a92863
Update testagent.py
KirbytroNic0528 Oct 11, 2024
f768ad9
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
88853da
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
12505b9
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
afdc122
Update coverage_and_test.yml
KirbytroNic0528 Oct 11, 2024
ab62eee
Merge branch 'main' into MoonBit-Test-Agent
tonyfettes Oct 11, 2024
9bd8ccb
Merge branch 'main' into MoonBit-Test-Agent
tonyfettes Oct 13, 2024
1393173
Add files via upload
KirbytroNic0528 Oct 14, 2024
274c190
Update coverage_and_test.yml
KirbytroNic0528 Oct 14, 2024
790d0fd
Update coverage_and_test.yml
KirbytroNic0528 Oct 14, 2024
3004f3e
Update coverage_and_test.yml
KirbytroNic0528 Oct 14, 2024
3bc771a
Update testagent.py
KirbytroNic0528 Oct 15, 2024
1d6060e
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
739d460
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
258c99a
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
5005c2a
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
59462c0
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
63d8f00
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
ed18621
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
fd7d1f2
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
7b9eed6
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
fbcc04c
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
259bc54
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
36276b7
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
7b1a02e
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
68e3e81
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
9f15f9c
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
219e1c2
Update coverage_and_test.yml
KirbytroNic0528 Oct 15, 2024
a340c03
Update coverage_and_test.yml
KirbytroNic0528 Oct 16, 2024
8a3293e
Update readcoverage.py
KirbytroNic0528 Oct 16, 2024
a658717
Update coverage_and_test.yml
KirbytroNic0528 Oct 16, 2024
cf5e5b5
Update coverage_and_test.yml
KirbytroNic0528 Oct 16, 2024
bb12c48
Update testagent.py
KirbytroNic0528 Oct 16, 2024
4184704
Update coverage_and_test.yml
KirbytroNic0528 Oct 16, 2024
2bac8fd
Update coverage_and_test.yml
KirbytroNic0528 Oct 16, 2024
3a3be85
Update coverage_and_test.yml
KirbytroNic0528 Oct 17, 2024
8590315
Update coverage_and_test.yml
KirbytroNic0528 Oct 17, 2024
d31dd70
Update coverage_and_test.yml
KirbytroNic0528 Oct 21, 2024
b1777a1
Add files via upload
KirbytroNic0528 Oct 28, 2024
d1d1ca7
Update gettest.py
KirbytroNic0528 Oct 28, 2024
3ccd47c
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
921d1df
Update testagent.py
KirbytroNic0528 Oct 28, 2024
16221df
Update writedown.py
KirbytroNic0528 Oct 28, 2024
a806d21
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
81e5026
Merge branch 'main' into MoonBit-Test-Agent
KirbytroNic0528 Oct 28, 2024
33bbce7
Update testagent.py
KirbytroNic0528 Oct 28, 2024
e132cbd
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
a05e348
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
68f2fde
Update testagent.py
KirbytroNic0528 Oct 28, 2024
fc4bfd7
Update requirements.txt
KirbytroNic0528 Oct 28, 2024
8ba79bb
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
766f02b
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
40cfc6e
Update requirements.txt
KirbytroNic0528 Oct 28, 2024
9b466c8
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
d2aef31
Update zhipuai.py
KirbytroNic0528 Oct 28, 2024
e8e5da2
Update zhipuai.py
KirbytroNic0528 Oct 28, 2024
fc1a2e0
Update writedown.py
KirbytroNic0528 Oct 28, 2024
ff999d1
Update gettest.py
KirbytroNic0528 Oct 28, 2024
d1ae519
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
4d33546
Update gettest.py
KirbytroNic0528 Oct 28, 2024
695c11b
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
edd5bd6
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
bab0ab5
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
e0453d0
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
ac4c362
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
5a095ee
Update gettest.py
KirbytroNic0528 Oct 28, 2024
e51fd0c
Update testagent.py
KirbytroNic0528 Oct 28, 2024
fe1c886
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
fbb94b0
Update testagent.py
KirbytroNic0528 Oct 28, 2024
112339e
Update testagent.py
KirbytroNic0528 Oct 28, 2024
0761a65
Add files via upload
KirbytroNic0528 Oct 28, 2024
808ef45
Update testagent.py
KirbytroNic0528 Oct 28, 2024
4346c59
Update writedown.py
KirbytroNic0528 Oct 28, 2024
ad5de51
Update gettest.py
KirbytroNic0528 Oct 28, 2024
c991054
Update and rename zhipuai.py to zhipuai_model.py
KirbytroNic0528 Oct 28, 2024
2febd8d
Update readcoverage.py
KirbytroNic0528 Oct 28, 2024
6ac2c08
Update coverage_and_test.yml
KirbytroNic0528 Oct 28, 2024
5267b75
Update main.py
KirbytroNic0528 Oct 29, 2024
1fd3b07
Update readme.md
KirbytroNic0528 Oct 29, 2024
44bb4ac
Update coverage_and_test.yml
KirbytroNic0528 Oct 29, 2024
ff0d308
Merge branch 'main' into MoonBit-Test-Agent
KirbytroNic0528 Oct 30, 2024
50bb8eb
Update coverage_and_test.yml
KirbytroNic0528 Oct 30, 2024
cd10baf
Merge branch 'main' into MoonBit-Test-Agent
tonyfettes Oct 31, 2024
f1d10b3
Update coverage_and_test.yml
KirbytroNic0528 Oct 31, 2024
d359c97
Update gettest.py
KirbytroNic0528 Oct 31, 2024
8de233b
Update readcoverage.py
KirbytroNic0528 Oct 31, 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
142 changes: 142 additions & 0 deletions .github/workflows/coverage_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
name: coverage_and_test

on:
push:
branches:
- main
pull_request:

jobs:
build:
strategy:
matrix:
os: [ ubuntu-latest ]
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4

- name: install
run: |
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
echo "$HOME/.moon/bin" >> $GITHUB_PATH
- name: moon version
run: |
moon version --all
moonrun --version

- name: moon check
run: moon check --deny-warn

- name: moon info
run: |
moon info
git diff --exit-code

- name: Set ulimit and run moon test
run: |
ulimit -s 8176
moon test --target all
moon test --release --target all

- name: moon bundle
run: moon bundle --all

- name: check core size
run: find ./target -name '*.core' | xargs ls -lh

- name: format diff
run: |
moon fmt
git diff --exit-code

- name: Set up Python
uses: actions/checkout@v4
with:
python-version: '3.9'

- name: Install dependencies
run: |
sudo apt install python3-pip
pip install -r scripts/requirements.txt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo? There is a script/requirement.txt in your PR.


test:
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4

- name: initial moon test
run: moon test --enable-coverage

- name: initial coverage report
run: |
moon coverage report -f summary summary > coverage_summary.txt
cat coverage_summary.txt >> "$GITHUB_STEP_SUMMARY"


- name: loop coverage improvement
id: loop-coverage
run: |
prev_coverage=$(cat coverage_summary.txt | grep 'Total Coverage' | awk '{print $NF}' | tr -d '%')
max_iterations=5
iteration=0
coverage_improved=true

while [ "$coverage_improved" = true ] && [ $iteration -lt $max_iterations ]; do
python Agent.py --api_key ${{ secrets.API_KEY }}

moon test --enable-coverage

moon coverage report -f summary summary > coverage_summary.txt

new_coverage=$(cat coverage_summary.txt | grep 'Total Coverage' | awk '{print $NF}' | tr -d '%')

if [ $(echo "$new_coverage > $prev_coverage" | bc) -eq 1 ]; then
prev_coverage=$new_coverage
echo "Coverage improved to $new_coverage%"
else
coverage_improved=false
echo "Coverage did not improve. Stopping loop."
fi
iteration=$((iteration + 1))
done

echo "Final coverage: $new_coverage%"
echo "::set-output name=final_coverage::$new_coverage"

- name: coverage report
run: |
cat coverage_summary.txt >> "$GITHUB_STEP_SUMMARY"
moon coverage report -f coveralls -o codecov_report.json --service-name github --service-job-id "$GITHUB_RUN_NUMBER" --service-pull-request "${{ github.event.number }}" --send-to coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}

typo-check:
runs-on: ubuntu-latest
timeout-minutes: 10
env:
FORCE_COLOR: 1
TYPOS_VERSION: v1.19.0
steps:
- name: download typos
run: curl -LsSf https://github.com/crate-ci/typos/releases/download/$TYPOS_VERSION/typos-$TYPOS_VERSION-x86_64-unknown-linux-musl.tar.gz | tar zxf - -C ${CARGO_HOME:-~/.cargo}/bin

- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: check typos
run: typos

license-header-check:
runs-on: ubuntu-latest
env:
HAWKEYE_VERSION: v5.5.1
steps:
- uses: actions/checkout@v4
- name: Download HawkEye
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/korandoru/hawkeye/releases/download/$HAWKEYE_VERSION/hawkeye-installer.sh | sh
- name: Check License Header
run: hawkeye check
36 changes: 36 additions & 0 deletions scripts/gettest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

def generate_test_code(moonbit_code,path,api_key):
test_prompt = ChatPromptTemplate.from_template(
"""作为一名MoonBit语言工程师,你的任务是编写一系列测试用例来验证项目的正确性。
请根据以下提供的格式,结合文件名{path}理解函数的用途,对给出的MoonBit函数编写对应的测试用例,
以下是你需要提供的测试用例格式参考:
test {{
assert_eq!(f(x))
assert_eq!(f(x))
}}
需要提供测试用例的MoonBit函数为{moonbit_code}。
注意,你的输出中只需要包含测试用例的代码,不需要包括分析过程以及任何其他语句。
同时,你生成的是moonbit语言的测试用例,请不要将moonbit语言与其他语言混淆
"""
)

test_llm = ChatZhipuAI(
api_key=api_key,
model="glm-4-plus",
temperature=0.5,
max_tokens=2048
)

test_retriever_chain = (
test_prompt
| test_llm
| StrOutputParser()
)
test_code = test_retriever_chain.invoke({
"moonbit_code": moonbit_code,
"path":path
})
return test_code
35 changes: 35 additions & 0 deletions scripts/readcoverage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

def read_coverage(moonbit_code, index, api_key):
read_prompt = ChatPromptTemplate.from_template(
"""你是一位技术高超的moonbit工程师,现在你需要读入moonbit语言的代码以及测试未覆盖代码行数的索引
根据索引找出测试未覆盖的代码并返回该行代码所在函数的函数
需要读入的MoonBit代码:{moonbit_code},未覆盖代码行数的索引:{index}
注意,你的输出中只需要包含未覆盖的函数,不需要包含分析过程和任何其他语句。
"""
)

read_llm = ChatZhipuAI(
api_key=api_key,
model="glm-4-plus",
temperature=0.5,
max_tokens=2048
)

read_retriever_chain = (
read_prompt
| read_llm
| StrOutputParser()
)
response = read_retriever_chain.invoke({
"moonbit_code": moonbit_code,
"index": index
})
return response





5 changes: 5 additions & 0 deletions scripts/requirement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
langchain
argparse
langchain_community
glob
pyjwt
30 changes: 30 additions & 0 deletions scripts/testagent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import argparse
import json
from Agent.readcoverage import read_coverage
from Agent.gettest import generate_test_code
from Agent.writedown import writedown_test_files
def main():
with open('coveralls.json', 'r') as file:
data = json.load(file)

parser = argparse.ArgumentParser(description="用于加载API密钥。")
parser.add_argument('--api_key', default="4a478b99108ee30c1ae4aaa0aefe6632.X8sj7A6gaBgWh9AE", type=str, help='API密钥')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the default key a valid key? Do I need to add variable API_KEY in the CI pipeline setting?

args = parser.parse_args()
zhipuai_api_key = args.api_key

for source_file in data['source_files']:
with open(source_file['name'], "r") as codefile:
index = [index for index, value in enumerate(source_file['coverage']) if value == 0]
if index!=[]:
print(index)
moonbit_code = codefile.read()
response = read_coverage(moonbit_code,index, zhipuai_api_key)
print("未覆盖的函数声明为"+response)
response = generate_test_code(response,source_file['name'],zhipuai_api_key)
test_code = response.replace("```moonbit\n", "").rstrip("```")
writedown_test_files(source_file['name'], test_code)
print("测试用例为"+test_code)


if __name__ == "__main__":
main()
7 changes: 7 additions & 0 deletions scripts/writedown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

def writedown_test_files(path, test_code):
with open(path, 'a') as file:
file.write(test_code + '\n')

print(f"test_code has been written to {path}")
Loading