From e77825e112ac4f32ed7cf72509954a9323526f1a Mon Sep 17 00:00:00 2001 From: nagaya Date: Wed, 13 Nov 2024 11:32:09 +0900 Subject: [PATCH 1/2] Add dev environments --- Dockerfile | 12 ++++++++++++ README.md | 11 +++++++---- Rakefile | 16 +++++++++++++++- docker-compose.yml | 44 ++++++++++++++++++++++++++++++++------------ spec/spec_helper.rb | 34 +++++++++++++++++++++++++--------- 5 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2a1bd9a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM ruby:3.1.6 + +WORKDIR /usr/src/app + +RUN mkdir -p /usr/src/app/lib/gratan +COPY Gemfile gratan.gemspec /usr/src/app/ +COPY lib/gratan/version.rb /usr/src/app/lib/gratan/ +RUN bundle install + +COPY . . + +ENTRYPOINT ["gratan"] diff --git a/README.md b/README.md index 4819a55..bb3ffd8 100644 --- a/README.md +++ b/README.md @@ -140,10 +140,13 @@ end ## Run tests ```sh -bundle install -docker-compose up -d -bundle exec rake -# MYSQL57=1 bundle exec rake +docker compose build +# for MySQL 5.6 and 5.7 +docker compose run --rm rake +# for MySQL 5.6 +docker compose run --rm rake spec5_6 +# for MySQL 5.7 +docker compose run --rm rake spec5_7 ``` ## Similar tools diff --git a/Rakefile b/Rakefile index 6adbdd9..1066d01 100644 --- a/Rakefile +++ b/Rakefile @@ -2,4 +2,18 @@ require 'bundler/gem_tasks' require 'rspec/core/rake_task' RSpec::Core::RakeTask.new('spec') -task :default => :spec +task :default => :spec_all + +suffixes = %w(5_6 5_7) +task :spec_all => suffixes.map { |s| "spec#{s}" } + +suffixes.each do |suffix| + overwrite_host = ENV['MYSQL_HOST'].to_s.empty? + task "spec#{suffix}" do + if overwrite_host + ENV['MYSQL_HOST'] = "mysql#{suffix}" + end + ENV['MYSQL5_7'] = (suffix == '5_7' ? 1 : 0).to_s + Rake::Task['spec'].execute + end +end diff --git a/docker-compose.yml b/docker-compose.yml index 26713e6..007b046 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,32 @@ -mysql56: - image: "mysql:5.6" - ports: - - "14406:3306" - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: "yes" -mysql57: - image: "mysql:5.7" - ports: - - "14407:3306" - environment: - MYSQL_ALLOW_EMPTY_PASSWORD: "yes" +services: + mysql5_6: + image: "mysql:5.6" + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + healthcheck: + test: ["CMD", "mysql", "-h", "localhost", "-u", "root", "--execute", "SHOW DATABASES;"] + start_period: 30s + timeout: 5s + interval: 5s + retries: 5 + mysql5_7: + image: "mysql:5.7" + environment: + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + healthcheck: + test: ["CMD", "mysql", "-h", "localhost", "-u", "root", "--execute", "SHOW DATABASES;"] + start_period: 30s + timeout: 5s + interval: 5s + retries: 5 + rake: + build: . + depends_on: + mysql5_6: + condition: service_healthy + mysql5_7: + condition: service_healthy + entrypoint: ["rake"] + volumes: + - .:/usr/src/app + working_dir: /usr/src/app diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e927f8b..459a6b5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,18 +13,34 @@ end end -def mysql57? - ENV['MYSQL57'] == '1' +def env_empty?(str) + str.nil? || str.empty? || str == '0' end -MYSQL_PORT = mysql57? ? 14407 : 14406 +def mysql5_7? + !env_empty?(ENV['MYSQL5_7']) +end + +MYSQL_PORT = if ENV['MYSQL_PORT'].blank? + 3306 + else + ENV['MYSQL_PORT'].to_i + end + +MYSQL_HOST = if ENV['MYSQL_HOST'].blank? + '127.0.0.1' + else + ENV['MYSQL_HOST'] + end + +MYSQL_USER = 'root' def mysql client = nil retval = nil begin - client = Mysql2::Client.new(host: '127.0.0.1', username: 'root', port: MYSQL_PORT) + client = Mysql2::Client.new(host: MYSQL_HOST, username: MYSQL_USER, port: MYSQL_PORT) retval = yield(client) ensure client.close if client @@ -127,7 +143,7 @@ def show_grants end end - if mysql57? + if mysql5_7? grants.each do |grant| end end @@ -141,14 +157,14 @@ def client(user_options = {}) end options = { - host: '127.0.0.1', - username: 'root', + host: MYSQL_HOST, + username: MYSQL_USER, port: MYSQL_PORT, ignore_user: IGNORE_USER, logger: Logger.new('/dev/null'), } - if mysql57? + if mysql5_7? options.update( override_sql_mode: true, use_show_create_user: true, @@ -189,7 +205,7 @@ def apply(cli = client) class Array def normalize - if mysql57? + if mysql5_7? self.map do |i| i.sub(/ IDENTIFIED BY PASSWORD '[^']+'/, '') .sub(/ REQUIRE \w+\b/, '') From 3aa174b6868adf889af0346130778c818cfa6799 Mon Sep 17 00:00:00 2001 From: nagaya Date: Wed, 13 Nov 2024 11:31:35 +0900 Subject: [PATCH 2/2] Add GitHub Action --- .github/workflows/test.yml | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..5d174fa --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,56 @@ +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + +permissions: + contents: read + pull-requests: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + check_test_execution_conditions: + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: xt0rted/block-autosquash-commits-action@v2 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + + rspec: + runs-on: ubuntu-latest + strategy: + matrix: + ruby: + - 3.0.6 + - 3.1.4 + mysql: + - 5.6 + - 5.7 + needs: + - check_test_execution_conditions + services: + mysql: + image: mysql:${{ matrix.mysql }} + env: + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + options: >- + --health-cmd "mysqladmin ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - "3306:3306" + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run RSpec for ${{ matrix.mysql }} + run: bundle exec rake spec`echo ${{ matrix.mysql }} | awk -F. '{print $1"_"$2}'` + env: + MYSQL_HOST: 127.0.0.1