Skip to content

Commit

Permalink
feat: prepare unit test files
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuadrini committed Aug 8, 2024
2 parents a51faa3 + 2acf6aa commit b97878d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 16 deletions.
23 changes: 23 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Release notes

## 0.0.3
Aug 08, 2024
- Add **prj_test** function
- Add user's bin path into **check_tool** function
- Fix return type to **prj_git** function
- Fix env path into files

## 0.0.2
Aug 02, 2024
- Add **prompt_confirm** function
- Add **prj_git** function
- Fix lowercase name of package

## 0.0.1
Jul 31, 2024
- Add **check_tool** function
- Add **make_dirs** function
- Add **make_file** function
- Add **prompt_text** function
- Add **prj_name** function
- Add _help_ message to **prompt_text** function
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "psp"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
authors = ["matteoguadrini"]

Expand Down
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@

```console
psp # Press Enter
Welcome to PSP (Python Scaffolding Projects): 0.0.2
Welcome to PSP (Python Scaffolding Projects): 0.0.3
> Name of Python project: test
> Do you want start git repository? Yes
> Do you want unit test files? Yes
Project `test` created
```

> This project is WIP
## Prerequisites

`psp` has three prerequisetes installed on own machine:
- git
- python3
- pip

## Installation

To install compiled file into your machine, download it:

```console
# For Linux
sudo curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.1/psp_linux > /usr/bin/psp
sudo chmod +x /usr/bin/psp
# For Linux (all users)
sudo -i
curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.3/psp_linux > /usr/bin/psp
chmod +x /usr/bin/psp

# For Linux (current user)
curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.3/psp_linux > $HOME/.local/bin/psp
chmod +x $HOME/.local/bin/psp

# For MacOS
sudo curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.1/psp_macos > /usr/bin/psp
sudo curl -L https://github.com/MatteoGuadrini/psp/releases/download/v0.0.3/psp_macos > /usr/bin/psp
sudo chmod +x /usr/bin/psp
```

Expand All @@ -37,7 +50,7 @@ cd psp && cargo build && sudo cp -var target/debug/psp /usr/bin/psp

- [x] Scaffolding file and folder structures for your Python project
- [x] Prepare git and gitignore
- [ ] Prepare unit test files (also with pytest)
- [x] Prepare unit test files (also with pytest)
- [ ] Prepare virtual environment
- [ ] Install dependencies
- [ ] Prepare pyproject.toml
Expand Down
76 changes: 67 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use inquire::{Confirm, Text};
use std::{
env::var,
fs::{create_dir_all, File},
io::Write,
path::Path,
process::exit,
};

// Constants
const VERSION: &str = "0.0.2";
const VERSION: &str = "0.0.3";

// Utility functions

// Function for check if tool is installed
fn check_tool(tool: &str) {
if !Path::new(tool).exists() {
eprintln!("error: the tool {} not installed", tool);
let home = var("HOME").unwrap();
let root_bin = format!("/usr/bin/{tool}");
let user_bin = format!("{home}/.local/bin/{tool}");
if !Path::new(&root_bin).exists() && !Path::new(&user_bin).exists() {
eprintln!("error: {} is not installed", tool);
exit(1);
}
}
Expand Down Expand Up @@ -88,7 +92,7 @@ fn prj_name() -> String {
// Make file structures
let file_ret = make_file(
format!("{project}/__init__.py").as_str(),
"#! /usr/env python3\n\n".to_string(),
"#! /usr/bin/env python3\n\n".to_string(),
);
match file_ret {
Err(e) => {
Expand All @@ -100,7 +104,8 @@ fn prj_name() -> String {
name
}

fn prj_git(name: &str) {
// Project git
fn prj_git(name: &str) -> bool {
let confirm = prompt_confirm("Do you want start git repository?", true, "None");
if confirm {
let output = std::process::Command::new("git")
Expand Down Expand Up @@ -149,11 +154,62 @@ fn prj_git(name: &str) {
/site\n"
.to_string(),
);
match file_ret {
let ret = match file_ret {
Err(e) => {
eprintln!("error: {}", e);
exit(5);
}
Ok(_) => true,
};
return ret;
}
false
}

// Project unit tests
fn prj_test(name: &str) {
let confirm = prompt_confirm("Do you want unit test files?", true, "None");
if confirm {
// Make directories structure
let dir_ret = make_dirs(format!("{name}/tests").as_str());
match dir_ret {
Err(e) => {
eprintln!("error: {}", e);
exit(6);
}
Ok(_) => (),
}
// Make file structures
let init_file = make_file(
format!("{name}/tests/__init__.py").as_str(),
"#! /usr/bin/env python3\n\n".to_string(),
);
match init_file {
Err(e) => {
eprintln!("error: {}", e);
exit(6);
}
Ok(_) => (),
}
let all_module = make_file(
format!("{name}/tests/all.py").as_str(),
format!(
"#! /usr/bin/env python3\n\n\n\
import unittest\n\n\n\
class TestAll(unittest.TestCase):\n\n\
\tdef test_all(self):\n\
\t\tprint('Test all {} successfully!')\n\n\n\
if __name__ == '__main__':\n\
\tunittest.main()",
name.to_lowercase()
)
.to_string(),
);
match all_module {
Err(e) => {
eprintln!("error: {}", e);
exit(6);
}
Ok(_) => (),
}
}
Expand All @@ -163,12 +219,14 @@ fn main() {
// Print welcome screen and version
println!("Welcome to PSP (Python Scaffolding Projects): {VERSION}");
// Check if Python 3 is installed
check_tool("/usr/bin/python3");
check_tool("python3");
// Create project structure by name
let name = prj_name();
// Start git
check_tool("/usr/bin/git");
prj_git(&name);
check_tool("git");
let _git = prj_git(&name);
// Unit tests
prj_test(&name);
// Finish scaffolding process
println!("Project `{name}` created")
}

0 comments on commit b97878d

Please sign in to comment.