Skip to content

Commit f8206a6

Browse files
Merge branch 'main' of https://github.com/Pythagora-io/pythagora into feat/expand-unit-tests
2 parents b867047 + 1d73001 commit f8206a6

File tree

6 files changed

+142
-97
lines changed

6 files changed

+142
-97
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Here are examples of open sourced repositories that we forked and created tests
5151

5252
- [Lodash](https://github.com/Pythagora-io/pythagora-demo-lodash)
5353
- 📝 1604 tests generated
54-
- 🐞 13 bugs found (3 edge cases and 10 bugs)
54+
- 🐞 11 bugs found (1 edge case and 10 bugs)
5555
- ⏳️ 4 hour run time
5656

5757
![lodash pythagora tests results](https://github.com/Pythagora-io/pythagora/assets/10895136/c0a2a589-e3ef-4812-9ea2-545307fd1a1d)
@@ -160,9 +160,6 @@ or to run tests from a specific file or a folder, run `npx jest <PATH_TO_FILE_OR
160160
<br><br>
161161
- **Tests help me think about my code - I don't want to generate them automatically**
162162
- That's the best thing about Pythagora - it actually does help you think about the code. Just, you don't need to spend time writing tests. This happened to us, who created Pythagora - we coded it as fast as possible but when we added unit test generation, we realized that it cannot create tests for some functions. So, we refactored the code and made it more modular so that unit tests can be generated for it.
163-
<br><br>
164-
- **What tests are the best**
165-
- That's the best thing about Pythagora - it actually does help you think about the code. Just, you don't need to spend time writing tests. This happened to us, who created Pythagora - we coded it as fast as possible but when we added unit test generation, we realized that it cannot create tests for some functions. So, we refactored the code and made it more modular so that unit tests can be generated for it.
166163

167164
<br>
168165

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pythagora",
3-
"version": "0.0.69",
3+
"version": "0.0.73",
44
"author": {
55
"name": "Zvonimir Sabljic",
66
"email": "[email protected]"
@@ -17,7 +17,8 @@
1717
"publish-pythagora": "src/bin/publish.bash",
1818
"prepublishOnly": "node src/bin/prepublish.js",
1919
"postinstall": "node src/bin/postinstall.js",
20-
"test": "npx jest ./pythagora_tests/ --coverage"
20+
"test": "npx jest ./pythagora_tests/ --coverage",
21+
"generate-unit-tests": "npx pythagora --unit-tests --path ./src"
2122
},
2223
"bin": {
2324
"pythagora": "src/bin/run.js"

src/bin/run.bash

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ yellow=$(tput setaf 3)
99
green=$(tput setaf 2)
1010
reset=$(tput sgr0)
1111
bold=$(tput bold)
12-
pythagora_dir="pythagora"
12+
pythagora_dir="$1"
1313

1414
function exit_handler {
1515
while [ ! -f "./.pythagora/finishingup" ]
@@ -25,26 +25,6 @@ function exit_handler {
2525
exit 0
2626
}
2727

28-
commands=(basename realpath dirname)
29-
found_all=true
30-
31-
for cmd in "${commands[@]}"; do
32-
if ! command -v "$cmd" >/dev/null 2>&1 ; then
33-
echo "$cmd command not found"
34-
found_all=false
35-
fi
36-
done
37-
38-
if [ "$found_all" = true ] ;
39-
then
40-
pythagora_dir=$(basename "$(dirname "$(dirname "$(dirname "$(realpath "$0")")")")")
41-
fi
42-
43-
if [ "$pythagora_dir" == "pythagora-dev" ]
44-
then
45-
pythagora_dir="@pythagora.io/pythagora-dev"
46-
fi
47-
4828
for (( i=0; i<${#args[@]}; i++ ))
4929
do
5030
if [[ "${args[$i]}" =~ ^--init[-_]command$ ]]
@@ -53,43 +33,35 @@ do
5333
elif [[ "${args[$i]}" == "--config" ]]
5434
then
5535
# TODO refactor and make it flexible
56-
CONFIG_DIR="./.pythagora"
57-
CONFIG_FILE="$CONFIG_DIR/config.json"
58-
TMP_FILE="$CONFIG_DIR/tmp.json"
36+
CONFIG_FILE="./.pythagora/config.json"
37+
rm "$CONFIG_FILE"
5938

60-
# Create the config file if it doesn't exist
6139
if [ ! -f "$CONFIG_FILE" ]; then
62-
mkdir -p $CONFIG_DIR
63-
echo "{}" > $CONFIG_FILE
40+
mkdir -p ./.pythagora
41+
touch "./$CONFIG_FILE"
6442
fi
6543

6644
API_NAME="${args[$i+1]//--/}"
6745
API_NAME="${API_NAME//-/_}"
6846
API_KEY="${args[$i+2]}"
69-
70-
if [ "$API_NAME" == "pythagora_api_key" ]; then
71-
jq 'del(.openai_api_key)' $CONFIG_FILE > $TMP_FILE && mv $TMP_FILE $CONFIG_FILE
72-
elif [ "$API_NAME" == "openai_api_key" ]; then
73-
jq 'del(.pythagora_api_key)' $CONFIG_FILE > $TMP_FILE && mv $TMP_FILE $CONFIG_FILE
74-
fi
75-
76-
# Use jq to add the new key-value pair to the JSON object
77-
jq --arg key "$API_NAME" --arg value "$API_KEY" '. + {($key): $value}' $CONFIG_FILE > $TMP_FILE && mv $TMP_FILE $CONFIG_FILE
47+
echo "{" >> $CONFIG_FILE
48+
echo " \"$API_NAME\": \"$API_KEY\"" >> $CONFIG_FILE
49+
echo "}" >> $CONFIG_FILE
7850
echo "${green}${bold}API key added to config!${reset}"
7951
exit 0
8052
elif [[ "${args[$i]}" == "--review" ]]
8153
then
82-
PYTHAGORA_CONFIG="$@" node "./node_modules/${pythagora_dir}/src/scripts/review.js"
54+
PYTHAGORA_CONFIG="$@" node "${pythagora_dir}/src/scripts/review.js"
8355
exit 0
8456
elif [[ "${args[$i]}" == "--tests-eligible-for-export" ]]
8557
then
8658
echo "${yellow}${bold}Tests eligible for export:${reset}"
87-
PYTHAGORA_CONFIG="$@" node "./node_modules/${pythagora_dir}/src/scripts/testsEligibleForExport.js"
59+
PYTHAGORA_CONFIG="$@" node "${pythagora_dir}/src/scripts/testsEligibleForExport.js"
8860
exit 0
8961
elif [[ "${args[$i]}" == "--unit-tests" ]]
9062
then
9163
echo "${green}${bold}Generating unit tests...${reset}"
92-
PYTHAGORA_CONFIG="$@" node "./node_modules/${pythagora_dir}/src/scripts/unit.js"
64+
PYTHAGORA_CONFIG="$@" node "${pythagora_dir}/src/scripts/unit.js"
9365
exit 0
9466

9567
elif [[ "${args[$i]}" == "--expand-unit-tests" ]]
@@ -100,23 +72,23 @@ do
10072

10173
elif [[ "${args[$i]}" == "--export-setup" ]]
10274
then
103-
PYTHAGORA_CONFIG="$@" node "./node_modules/${pythagora_dir}/src/scripts/enterData.js"
75+
PYTHAGORA_CONFIG="$@" node "${pythagora_dir}/src/scripts/enterData.js"
10476
exit 0
10577
elif [[ "${args[$i]}" =~ ^--rename[-_]tests$ ]]
10678
then
107-
node "./node_modules/${pythagora_dir}/src/scripts/renameTests.js"
79+
node "${pythagora_dir}/src/scripts/renameTests.js"
10880
exit 0
10981
elif [[ "${args[$i]}" =~ ^--delete[-_]all[-_]failed$ ]]
11082
then
111-
node "./node_modules/${pythagora_dir}/src/scripts/deleteAllFailed.js"
83+
node "${pythagora_dir}/src/scripts/deleteAllFailed.js"
11284
exit 0
11385
elif [[ "${args[$i]}" == "--delete" ]]
11486
then
115-
PYTHAGORA_CONFIG="$@" node "./node_modules/${pythagora_dir}/src/scripts/deleteTest.js"
87+
PYTHAGORA_CONFIG="$@" node "${pythagora_dir}/src/scripts/deleteTest.js"
11688
exit 0
11789
elif [[ "${args[$i]}" == "--export" ]]
11890
then
119-
PYTHAGORA_CONFIG="$@" node -e "require('./node_modules/${pythagora_dir}/src/commands/export.js').runExport()"
91+
PYTHAGORA_CONFIG="$@" node -e "require('${pythagora_dir}/src/commands/export.js').runExport()"
12092
exit 0
12193
elif [[ "${args[$i]}" == "--mode" ]]
12294
then
@@ -133,7 +105,7 @@ fi
133105
if [[ " ${args[@]} " =~ " --no-code-coverage " ]] || ([[ ! " ${args[@]} " =~ " --mode test " ]] && [[ ! " ${args[@]} " =~ " --mode=test " ]])
134106
then
135107
args=( "${args[@]//--no-code-coverage/}" )
136-
PYTHAGORA_CONFIG="$@" NODE_OPTIONS="${inspect} --require ./node_modules/${pythagora_dir}/src/RunPythagora.js" $init_command &
108+
PYTHAGORA_CONFIG="$@" NODE_OPTIONS="${inspect} --require ${pythagora_dir}/src/RunPythagora.js" $init_command &
137109
else
138110
nyc_args=( "--reporter=text-summary" )
139111

@@ -144,11 +116,11 @@ else
144116
nyc_args+=( "--report-dir=./pythagora_tests/code_coverage_report" )
145117
fi
146118

147-
if [ -f "./node_modules/$pythagora_dir/node_modules/nyc/bin/nyc.js" ]
119+
if [ -f "$pythagora_dir/node_modules/nyc/bin/nyc.js" ]
148120
then
149-
PYTHAGORA_CONFIG="$@" NODE_OPTIONS="${inspect} --require ./node_modules/${pythagora_dir}/src/RunPythagora.js" ./node_modules/"$pythagora_dir"/node_modules/nyc/bin/nyc.js "${nyc_args[@]}" $init_command &
121+
PYTHAGORA_CONFIG="$@" NODE_OPTIONS="${inspect} --require ${pythagora_dir}/src/RunPythagora.js" "$pythagora_dir"/node_modules/nyc/bin/nyc.js "${nyc_args[@]}" $init_command &
150122
else
151-
PYTHAGORA_CONFIG="$@" NODE_OPTIONS="${inspect} --require ./node_modules/${pythagora_dir}/src/RunPythagora.js" ./node_modules/nyc/bin/nyc.js "${nyc_args[@]}" $init_command &
123+
PYTHAGORA_CONFIG="$@" NODE_OPTIONS="${inspect} --require ${pythagora_dir}/src/RunPythagora.js" ./node_modules/nyc/bin/nyc.js "${nyc_args[@]}" $init_command &
152124
fi
153125

154126
fi

src/bin/run.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ exec(bashCommand, (error, stdout, stderr) => {
3838
const bashPaths = stdout.split('\r\n').filter(line => line.trim() !== '').map(line => line.trim());
3939
const bashPath = bashPaths.find((p) => fs.existsSync(p));
4040
const bashScript = winOS ? process.argv[1].replace('run.js', 'run.bash') : path.join(__dirname, 'run.bash');
41+
const scriptDir = path.dirname(bashScript); // Get the directory of the bash script
42+
const pythagoraDir = path.dirname(path.dirname(scriptDir)); // Get the pythagora directory
4143
const args = process.argv.slice(2);
4244

4345
// Run the bash script and forward all arguments
44-
const child = spawn(bashPath, [bashScript, ...args], { stdio: 'inherit' });
46+
const child = spawn(bashPath, [bashScript, pythagoraDir, ...args], { stdio: 'inherit' });
4547

4648
child.on('error', (error) => {
4749
console.error(`Error running the bash script: ${error.message}`);

0 commit comments

Comments
 (0)