Skip to content

Commit 4cf22fb

Browse files
authored
partial test and code format during pkg test (#34)
* improve test and code format - add code format in pkg test. The code format will only run incrementally based on `git diff`, so it's much faster - do an incremental test on problems locally (also based on `git diff`). CI still tests against all problems. - run each problem in a sandbox module so it won't get affected by other codes. - remove auto format CI since format is run locally during pkg test * test/format based on git diff HEAD~1 * use Github Action for unit test * move format_utils to test * 1.3 compat
1 parent d2bafc1 commit 4cf22fb

File tree

10 files changed

+159
-75
lines changed

10 files changed

+159
-75
lines changed

.github/workflows/UnitTest.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Unit test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
schedule:
9+
- cron: '20 00 1 * *'
10+
11+
jobs:
12+
test:
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
julia-version: ['1.3', '1', 'nightly']
18+
os: [ubuntu-latest, windows-latest, macOS-latest]
19+
20+
steps:
21+
- uses: actions/[email protected]
22+
- name: "Set up Julia"
23+
uses: julia-actions/setup-julia@v1
24+
with:
25+
version: ${{ matrix.julia-version }}
26+
27+
- name: Cache artifacts
28+
uses: actions/cache@v1
29+
env:
30+
cache-name: cache-artifacts
31+
with:
32+
path: ~/.julia/artifacts
33+
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
34+
restore-keys: |
35+
${{ runner.os }}-test-${{ env.cache-name }}-
36+
${{ runner.os }}-test-
37+
${{ runner.os }}-
38+
- name: "Unit Test"
39+
uses: julia-actions/julia-runtest@master
40+
41+
- uses: julia-actions/julia-processcoverage@v1
42+
- uses: codecov/codecov-action@v1
43+
with:
44+
file: lcov.info

.github/workflows/format_pr.yml

Lines changed: 0 additions & 37 deletions
This file was deleted.

.travis.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
1212
[compat]
1313
BenchmarkTools = "0.5"
1414
DataStructures = "0.18"
15+
JuliaFormatter = "0.10"
1516
julia = "1.3"
1617

1718
[extras]
19+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
20+
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
1821
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1922

2023
[targets]
21-
test = ["Test"]
24+
test = ["JuliaFormatter", "LibGit2", "Test"]

README-zh-cn.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
# LeetCode in Julia - 用 Julia 编写力扣题目
22

3-
[![Build Status](https://travis-ci.com/JuliaCN/LeetCode.jl.svg?branch=master)](https://travis-ci.com/JuliaCN/LeetCode.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
3+
[![Unit Test][action-img]][action-url]
4+
[![Code Style: Blue][blue-img]][blue-url]
45

56
社区驱动的项目:用 Julia 做 LeetCode 题目
67

78
## 如何做出贡献?
89

910
1. [Fork](https://guides.github.com/activities/forking/) 本项目
10-
1. 使用 `git clone` 本项目
11-
1.`src/problems/` 路径下新建一个文件夹。
11+
2. 使用 `git clone` 本项目
12+
3.`src/problems/` 路径下新建一个文件夹。
1213
你可能会想要使用像 [leetcode-cli](https://github.com/skygragon/leetcode-cli) 这样辅助工具,
1314
通过 `leetcode show 1 -g -x -l python` 命令来生成一个 `python` 的模板,并将其重命名为 `.jl` 文件。
1415
(如果你使用 VSCode 你也可能需要 [LeetCode VSCode 插件](https://marketplace.visualstudio.com/items?itemName=LeetCode.vscode-leetcode)
15-
1.`test/problems` 文件夹下编写测试样例,确保你的解答能满足题目的要求。
16-
1. 对于一些共同的组件,将他们放到 `src/common.jl` 中,并在 `test/common.jl` 里添加测试。
17-
1. 提交(commit)你的更改,并开启一个新的合并请求(pull request)。
18-
1. 你还可以 **关注(WATCH)** 此项目以帮助审阅其他人的合并请求,或者 **点个赞(STAR)** 以鼓励本项目的贡献者们。
16+
4.`test/problems` 文件夹下编写测试样例,确保你的解答能满足题目的要求。
17+
5. 对于一些共同的组件,将他们放到 `src/common.jl` 中,并在 `test/common.jl` 里添加测试。
18+
6. 通过 `pkg> test` 测试结果是否正确, 测试过程会格式化代码
19+
7. 提交(commit)你的更改,并开启一个新的合并请求(pull request)。
20+
8. 你还可以 **关注(WATCH)** 此项目以帮助审阅其他人的合并请求,或者 **点个赞(STAR)** 以鼓励本项目的贡献者们。
21+
22+
[action-img]: https://github.com/JuliaCN/LeetCode.jl/workflows/Unit%20test/badge.svg
23+
[action-url]: https://github.com/JuliaCN/LeetCode.jl/actions
24+
[blue-img]: https://img.shields.io/badge/code%20style-blue-4495d1.svg
25+
[blue-url]: https://github.com/invenia/BlueStyle

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# LeetCode
22

3-
[![Build Status](https://travis-ci.com/JuliaCN/LeetCode.jl.svg?branch=master)](https://travis-ci.com/JuliaCN/LeetCode.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
3+
[![Unit Test][action-img]][action-url]
4+
[![Code Style: Blue][blue-img]][blue-url]
45

56

67
[中文说明](README-zh-cn.md)
@@ -10,8 +11,14 @@ A community driven project to provide solutions for LeetCode problems in the Jul
1011
## How to contribute?
1112

1213
1. [Fork](https://guides.github.com/activities/forking/) this project.
13-
1. Create a new problem file under the `src/problems/` folder. You may want to use tools like [leetcode-cli](https://github.com/skygragon/leetcode-cli) with the command like `leetcode show 1 -g -x -l python` to generate a `python` template and then modify the code and rename it into a `.jl` file. (You may also consider [LeetCode VSCode Plugin](https://marketplace.visualstudio.com/items?itemName=LeetCode.vscode-leetcode) if you use VSCode)
14-
1. Write test cases inside the `test/problems` folder to make sure the solutions work as expected.
15-
1. For some common components, please put them into the `src/common.jl` file and add test cases in `test/common.jl`.
16-
1. Commit your changes and make a pull request.
17-
1. You may also **WATCH** this project to help review other's PR or **STAR** this project to spread JuliaLang to others.
14+
2. Create a new problem file under the `src/problems/` folder. You may want to use tools like [leetcode-cli](https://github.com/skygragon/leetcode-cli) with the command like `leetcode show 1 -g -x -l python` to generate a `python` template and then modify the code and rename it into a `.jl` file. (You may also consider [LeetCode VSCode Plugin](https://marketplace.visualstudio.com/items?itemName=LeetCode.vscode-leetcode) if you use VSCode)
15+
3. Write test cases inside the `test/problems` folder to make sure the solutions work as expected.
16+
4. For some common components, please put them into the `src/common.jl` file and add test cases in `test/common.jl`.
17+
5. Run `pkg> test` locally to make sure test passes. The test will also format your codes.
18+
6. Commit your changes and make a pull request.
19+
7. You may also **WATCH** this project to help review other's PR or **STAR** this project to spread JuliaLang to others.
20+
21+
[action-img]: https://github.com/JuliaCN/LeetCode.jl/workflows/Unit%20test/badge.svg
22+
[action-url]: https://github.com/JuliaCN/LeetCode.jl/actions
23+
[blue-img]: https://img.shields.io/badge/code%20style-blue-4495d1.svg
24+
[blue-url]: https://github.com/invenia/BlueStyle

test/format_utils.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using LibGit2
2+
using LibGit2.Consts: DELTA_ADDED, DELTA_MODIFIED, DELTA_DELETED
3+
using JuliaFormatter
4+
5+
const DEFAULT_DIFF_FILTER = Set((
6+
LibGit2.Consts.DELTA_ADDED,
7+
LibGit2.Consts.DELTA_MODIFIED,
8+
# LibGit2.Consts.DELTA_DELETED # only need to format existing files
9+
))
10+
11+
# modified based on LibGit2.diff_files
12+
function diff_files(repo, tree, pathspecs=""; filter=DEFAULT_DIFF_FILTER)
13+
tree = LibGit2.GitTree(repo, tree * "^{tree}")
14+
files = String[]
15+
try
16+
diff = LibGit2.diff_tree(repo, tree, pathspecs)
17+
for i in 1:LibGit2.count(diff)
18+
delta = diff[i]
19+
delta === nothing && break
20+
if LibGit2.Consts.DELTA_STATUS(delta.status) in filter
21+
push!(files, unsafe_string(delta.new_file.path))
22+
end
23+
end
24+
close(diff)
25+
catch
26+
close(tree)
27+
end
28+
return files
29+
end
30+
31+
"""
32+
format_diff_file(root, tree="HEAD", pathspecs=""; filter)
33+
34+
Apply JuliaFormatter to git diff (.jl) files.
35+
36+
# Inputs
37+
38+
- `root::String`: the root path to the git repo
39+
- `tree::String`: git tree spec
40+
- `pathspecs::String=""`: same to `git diff -- <pathspecs>`
41+
- `filter=identity`: a function that used to filter out files of interested.
42+
"""
43+
function format_diff_file(root::String, tree="HEAD", pathspecs::String=""; filter=identity)
44+
repo = LibGit2.GitRepo(root)
45+
46+
jl_diff = Base.filter(
47+
x -> endswith(x, ".jl"), joinpath.(root, diff_files(repo, tree, pathspecs))
48+
)
49+
for file in filter(jl_diff)
50+
format(file, BlueStyle(); verbose=true)
51+
end
52+
end

test/problems/problems.jl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
@testset "problems" begin
2-
for f in readdir(@__DIR__)
3-
if f != splitdir(@__FILE__)[2]
4-
include(f)
2+
is_ci = get(ENV, "CI", nothing) == "true"
3+
if is_ci
4+
test_files = filter(isfile, joinpath.(@__DIR__, readdir(@__DIR__)))
5+
else
6+
repo = LibGit2.GitRepo(root)
7+
problem_dir = relpath(@__DIR__, root) # "test/problems"
8+
9+
test_files = filter(diff_files(repo, "HEAD~1", "")) do f
10+
startswith(f, problem_dir) && endswith(f, ".jl")
511
end
612
end
13+
# don't recursively include "problem.jl" itself
14+
test_files = filter(x -> x != "problems.jl", basename.(test_files))
15+
16+
@info "testing $(length(test_files)) problems" CI = is_ci
17+
for f in test_files
18+
# run tests in a sandbox
19+
m = Module(gensym())
20+
# modules created with Module() does not have include defined
21+
Core.eval(m, :(include(x) = Base.include($m, x)))
22+
Core.eval(m, :(include("../requirements.jl")))
23+
Core.eval(m, :(include($f)))
24+
end
725
end

test/requirements.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
using LeetCode
2+
using Test

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
using LeetCode
22
using Test
3+
include("format_utils.jl")
4+
5+
# Every problem is run in a sandbox environment, test requirements are put into "requirements.jl"
6+
# and will be included for each single problem.
7+
8+
root = abspath(@__DIR__, "..")
39

410
@testset "LeetCode.jl" begin
511
include("common.jl")
612
include("problems/problems.jl")
713
end
14+
15+
# trigger an auto-format after test
16+
format_diff_file(root, "HEAD~1")

0 commit comments

Comments
 (0)