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

Refactor ci_generate.py #1034

Merged
merged 14 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions .github/workflows/all-generate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: All generate test

on:
schedule:
- cron: "0 0 * * *"

jobs:
deploy:
uses: ./.github/workflows/generate.yml
with:
force-generate: true
11 changes: 11 additions & 0 deletions .github/workflows/diff-generate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: Diff only generate test

on:
push:
branches:
- master
pull_request:

jobs:
deploy:
uses: ./.github/workflows/generate.yml
133 changes: 133 additions & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: Generate test

on:
workflow_call:
inputs:
force-generate:
description: Ignore cache and force generate
required: false
default: false
type: boolean
enable-gcc-ubuntu-diffrun:
description: Enable gcc-ubuntu-diffrun
required: false
default: true
type: boolean
enable-clang-mac-diffrun:
description: Enable clang-mac-diffrun
required: false
default: true
type: boolean
enable-mingw-windows-diffrun:
description: Enable mingw-windows-diffrun
required: false
default: true
type: boolean

jobs:
gcc-ubuntu-diffrun:
runs-on: ubuntu-latest
if: ${{ github.event.inputs.skip-gcc-ubuntu-diffrun != 'true' }}
steps:
- uses: actions/checkout@v3

- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --user -r requirements.txt

- name: Restore versions.json
uses: actions/cache/restore@v3
if: ${{ github.event.inputs.force-generate != 'true' }}
with:
path: versions.json
key: ${{ runner.os }}-versions-cache-

- name: Run generate.py
run: |
ulimit -s unlimited
./generate_test.py TestGenerateAll
env:
ENABLE_GENERATE_TEST: 1
VERSIONS_CACHE_PATH: versions.json

- name: Save versions.json
uses: actions/cache/save@v3
with:
path: versions.json
key: ${{ runner.os }}-versions-cache-${{ hashFiles('versions.json') }}

clang-mac-diffrun:
runs-on: macos-latest
if: ${{ github.event.inputs.skip-clang-mac-diffrun != 'true' }}
steps:
- uses: actions/checkout@v3

- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --user -r requirements.txt

- name: Restore versions.json
uses: actions/cache/restore@v3
if: ${{ github.event.inputs.force-generate != 'true' }}
with:
path: versions.json
key: ${{ runner.os }}-versions-cache-

- name: Run generate.py
run: |
./generate_test.py TestGenerateAll
env:
CXX: clang++
ENABLE_GENERATE_TEST: 1
VERSIONS_CACHE_PATH: versions.json

- name: Save versions.json
uses: actions/cache/save@v3
with:
path: versions.json
key: ${{ runner.os }}-versions-cache-${{ hashFiles('versions.json') }}

mingw-windows-diffrun:
runs-on: windows-latest
if: ${{ github.event.inputs.skip-mingw-windows-diffrun != 'true' }}
steps:
- uses: actions/checkout@v3

- name: Set up Python 3.x
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install --user -r requirements.txt

- name: Restore versions.json
uses: actions/cache/restore@v3
if: ${{ github.event.inputs.force-generate != 'true' }}
with:
path: versions.json
key: ${{ runner.os }}-versions-cache-

- name: Run generate.py
run: |
python generate_test.py TestGenerateAll
env:
ENABLE_GENERATE_TEST: 1
VERSIONS_CACHE_PATH: versions.json

- name: Save versions.json
uses: actions/cache/save@v3
with:
path: versions.json
key: ${{ runner.os }}-versions-cache-${{ hashFiles('versions.json') }}
44 changes: 43 additions & 1 deletion generate_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python3

import unittest
import json
import colorlog
from logging import basicConfig, getLogger
from os import chdir, getenv
from subprocess import run, check_output
Expand All @@ -11,6 +13,33 @@
from typing import List
logger = getLogger(__name__)

@unittest.skipIf(getenv('ENABLE_GENERATE_TEST') == None, "generate test take long time")
class TestGenerateAll(unittest.TestCase):
def test_generate_all(self):
tomls = list(filter(lambda p: not p.match('test/**/info.toml'), Path('.').glob('**/info.toml')))
tomls = sorted(tomls, key=lambda x: x.parent.name)

cache_path = Path(getenv('VERSIONS_CACHE_PATH'))
versions = json.load(open(cache_path, 'r')) if cache_path.exists() else dict()

for toml in tomls:
problem = Problem(Path.cwd(), toml.parent)
name = problem.basedir.name
version = problem.problem_version()

with self.subTest(name=name):
if versions.get(name) == version:
logger.info('Skip generated problem: {}'.format(name))
else:
logger.info('Generate {}'.format(name))
problem.generate(mode=Problem.Mode.TEST)
problem.generate(mode=Problem.Mode.CLEAN)
versions[name] = version

with open(cache_path, 'w') as f:
json.dump(versions, f)



def create_test_dir(problem_name: str) -> TemporaryDirectory:
problem_dir = Path('test') / problem_name
Expand Down Expand Up @@ -236,8 +265,21 @@ def test_list_depending_files(self):
self.assertTrue(find_verifier)

if __name__ == "__main__":
handler = colorlog.StreamHandler()
formatter = colorlog.ColoredFormatter(
"%(log_color)s%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
log_colors={
'DEBUG': 'cyan',
'INFO': 'white',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
})
handler.setFormatter(formatter)
basicConfig(
level=getenv('LOG_LEVEL', 'DEBUG'),
format="%(asctime)s %(levelname)s %(name)s : %(message)s"
handlers=[handler]
)

unittest.main()