old_contents: import yaml
  
  
  def test_get_docker_compose_yml_extra_config():
      """Make sure all values passed in config make their way to the
      web and worker container environment
      """
      result = get_yaml({"foo": "bar"})
      assert result["services"]["web"]["environment"]["foo"] == "bar"
  
  
  def test_get_docker_compose_yml_core_config():
      """Make sure worker and web services have the necessary variables to run"""
      result = get_yaml({})
      assert "REDIS_URL" in result["services"]["worker"]["environment"]
      assert "DATABASE_URL" in result["services"]["worker"]["environment"]
      assert "HOME" in result["services"]["worker"]["environment"]
  
  
  def test_get_docker_compose_yml_env_vars_always_strings():
      """The docker-compose.yml file we generate should always have strings as
      values in the `environment` section of each service.
      """
      result = get_yaml({"foo": True, "bar": 2})
      assert result["services"]["worker"]["environment"]["foo"] == "True"
      assert result["services"]["worker"]["environment"]["bar"] == "2"
  
  
  def test_get_docker_compose_yml_env_vars_escaping():
      """Environment vars with special character should be correctly escaped."""
      result = get_yaml(
          {
              "foo": r'" a quote and a \ backslash ',
          }
      )
      assert (
          result["services"]["worker"]["environment"]["foo"]
          == r'" a quote and a \ backslash '
      )
  
  
  def test_add_image_name(tmp_path):
      from dallinger.command_line.docker import add_image_name
  
      file = tmp_path / "test.txt"
  
      file.write_text("")
      add_image_name(str(file), "foobar")
      assert "image_name = foobar" in file.read_text()
  
      file.write_text("\nimage_name = old_image_name\n")
      add_image_name(str(file), "new_image_name")
      assert "old_image_name" not in file.read_text()
      assert "image_name = new_image_name" in file.read_text()
  
      file.write_text("foo = bar\nimage_base_name = the_base_image_name\nbar = foo")
      add_image_name(str(file), "foobar_image")
      assert (
          file.read_text()
          == "foo = bar\nimage_base_name = the_base_image_name\nimage_name = foobar_image\nbar = foo"
      )
  
  
  def get_yaml(config):
      from dallinger.command_line.docker_ssh import get_docker_compose_yml
  
      yaml_contents = get_docker_compose_yml(
          config, "dlgr-8c43a887", "ghcr.io/dallinger/dallinger/bartlett1932", "foobar"
      )
      return yaml.load(yaml_contents)
  
new_contents: from pathlib import Path
  import yaml
  
  
  def test_get_docker_compose_yml_extra_config():
      """Make sure all values passed in config make their way to the
      web and worker container environment
      """
      result = get_yaml({"foo": "bar"})
      assert result["services"]["web"]["environment"]["foo"] == "bar"
  
  
  def test_get_docker_compose_yml_core_config():
      """Make sure worker and web services have the necessary variables to run"""
      result = get_yaml({})
      assert "REDIS_URL" in result["services"]["worker"]["environment"]
      assert "DATABASE_URL" in result["services"]["worker"]["environment"]
      assert "HOME" in result["services"]["worker"]["environment"]
  
  
  def test_get_docker_compose_yml_env_vars_always_strings():
      """The docker-compose.yml file we generate should always have strings as
      values in the `environment` section of each service.
      """
      result = get_yaml({"foo": True, "bar": 2})
      assert result["services"]["worker"]["environment"]["foo"] == "True"
      assert result["services"]["worker"]["environment"]["bar"] == "2"
  
  
  def test_get_docker_compose_yml_env_vars_escaping():
      """Environment vars with special character should be correctly escaped."""
      result = get_yaml(
          {
              "foo": r'" a quote and a \ backslash ',
          }
      )
      assert (
          result["services"]["worker"]["environment"]["foo"]
          == r'" a quote and a \ backslash '
      )
  
  
  def test_add_image_name(tempdir):
      from dallinger.command_line.docker import add_image_name
  
      file = Path(tempdir) / "test.txt"
  
      file.write_text("")
      add_image_name(str(file), "foobar")
      assert "image_name = foobar" in file.read_text()
  
      file.write_text("\nimage_name = old_image_name\n")
      add_image_name(str(file), "new_image_name")
      assert "old_image_name" not in file.read_text()
      assert "image_name = new_image_name" in file.read_text()
  
      file.write_text("foo = bar\nimage_base_name = the_base_image_name\nbar = foo")
      add_image_name(str(file), "foobar_image")
      assert (
          file.read_text()
          == "foo = bar\nimage_base_name = the_base_image_name\nimage_name = foobar_image\nbar = foo"
      )
  
  
  def get_yaml(config):
      from dallinger.command_line.docker_ssh import get_docker_compose_yml
  
      yaml_contents = get_docker_compose_yml(
          config, "dlgr-8c43a887", "ghcr.io/dallinger/dallinger/bartlett1932", "foobar"
      )
      return yaml.load(yaml_contents)
  
subject: Use fixture tempdir from dallinger instead of tmp_path from pytest