From 41f0d21f89f2f53e82a54486d5f7f1e8423fd998 Mon Sep 17 00:00:00 2001 From: Chad Woolley Date: Sun, 1 May 2022 21:34:30 -0700 Subject: [PATCH] Add support for SPRING_QUIET environment variable. Closes #467 --- CHANGELOG.md | 2 ++ README.md | 8 ++++++++ lib/spring/configuration.rb | 7 ++++++- test/support/acceptance_test.rb | 7 ++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cfdd871..be54c0b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Next Release +* Add support for `SPRING_QUIET` environment variable. + ## 4.0.0 * Stop requiring `set` before bundler can select the proper version. This could result in diff --git a/README.md b/README.md index 5266cafd..990c6f57 100644 --- a/README.md +++ b/README.md @@ -388,6 +388,12 @@ a command runs: Spring.quiet = true ``` +You can also set the initial state of the `quiet` configuration option to true +by setting the `SPRING_QUIET` environment variable before executing Spring. +This is useful if you want to set quiet mode when invoking the Spring executable +in a subprocess, and cannot or prefer not to set it programmatically +via the `Spring.quiet` option in `~/.spring.rb` or the app's `config/spring.rb`. + ### Environment variables The following environment variables are used by Spring: @@ -412,6 +418,8 @@ The following environment variables are used by Spring: the long-running Spring server process. By default, this is related to the socket path; if the socket path is `/foo/bar/spring.sock` the pidfile will be `/foo/bar/spring.pid`. +* `SPRING_QUIET` - If set, the initial state of the `Spring.quiet` + configuration option will default to `true`. * `SPRING_SERVER_COMMAND` - The command to run to start up the Spring server when it is not already running. Defaults to `spring _[version]_ server --background`. diff --git a/lib/spring/configuration.rb b/lib/spring/configuration.rb index 0b57c46c..321ba1a0 100644 --- a/lib/spring/configuration.rb +++ b/lib/spring/configuration.rb @@ -2,7 +2,8 @@ module Spring class << self - attr_accessor :application_root, :quiet + attr_accessor :application_root + attr_writer :quiet def gemfile require "bundler" @@ -52,6 +53,10 @@ def project_root_path @project_root_path ||= find_project_root(Pathname.new(File.expand_path(Dir.pwd))) end + def quiet + @quiet ||= ENV.key?('SPRING_QUIET') + end + private def find_project_root(current_dir) diff --git a/test/support/acceptance_test.rb b/test/support/acceptance_test.rb index 76d1628c..d30e45cb 100644 --- a/test/support/acceptance_test.rb +++ b/test/support/acceptance_test.rb @@ -125,12 +125,17 @@ def without_gem(name) assert_success app.spring_test_command, stderr: "Running via Spring preloader in process" end - test "does not tell the user that Spring is being used when used automatically via binstubs but quiet is enabled" do + test "does not tell the user that Spring is being used when quiet is enabled via Spring.quiet" do File.write("#{app.user_home}/.spring.rb", "Spring.quiet = true") assert_success "bin/rails runner ''" refute_output_includes "bin/rails runner ''", stderr: 'Running via Spring preloader in process' end + test "does not tell the user that Spring is being used when quiet is enabled via SPRING_QUIET ENV var" do + assert_success "SPRING_QUIET=true bin/rails runner ''" + refute_output_includes "bin/rails runner ''", stderr: 'Running via Spring preloader in process' + end + test "raises if config.cache_classes is true" do config_path = app.path("config/environments/development.rb") config = File.read(config_path)