import json

from dmaws.stacks import Stack
from dmaws.context import Context


def is_true(x):
    assert x


def is_in(a, b):
    assert a in b


def valid_stack_json(stack):
    text = stack.build('stage', 'env', {}).template_body
    template = json.loads(text)

    assert 'Parameters' in template
    assert set(template['Parameters']) == set(stack.parameters)
    assert 'Resources' in template


def test_stack_definitions():
    ctx = Context()
    ctx.load_stacks('stacks.yml')

    yield('Found stacks in the stacks.yml',
          is_true, any(isinstance(s, Stack) for s in ctx.stacks.values()))
    yield('Found groups in stacks.yml',
          is_true, any(isinstance(s, list) for s in ctx.stacks.values()))

    for name, stack in ctx.stacks.items():
        if isinstance(stack, list):
            for s in stack:
                yield('Stack "%s" in group %s is defined' % (s, name),
                      is_in, s, ctx.stacks)
        else:
            for s in stack.dependencies:
                yield('%s dependency "%s" is defined' % (name, s),
                      is_in, s, ctx.stacks)
            yield('Stack "%s" template_body is valid JSON' % name,
                  valid_stack_json, stack)

import os
import json

from dmaws.stacks import Stack
from dmaws.context import Context


def is_true(x):
    assert x


def is_in(a, b):
    assert a in b


def valid_stack_json(ctx, stack):
    text = stack.build('stage', 'env', ctx.variables).template_body
    template = json.loads(text)

    assert 'Parameters' in template
    assert set(template['Parameters']) == set(stack.parameters)
    assert 'Resources' in template


def _load_default_vars(ctx):
    default_vars_files = [
        'vars/common.yml',
    ]
    if os.path.exists('vars/user.yml'):
        default_vars_files.append('vars/user.yml')

    ctx.load_variables(files=default_vars_files)


def test_stack_definitions():
    ctx = Context()
    _load_default_vars(ctx)
    ctx.load_stacks('stacks.yml')

    yield('Found stacks in the stacks.yml',
          is_true, any(isinstance(s, Stack) for s in ctx.stacks.values()))
    yield('Found groups in stacks.yml',
          is_true, any(isinstance(s, list) for s in ctx.stacks.values()))

    for name, stack in ctx.stacks.items():
        if isinstance(stack, list):
            for s in stack:
                yield('Stack "%s" in group %s is defined' % (s, name),
                      is_in, s, ctx.stacks)
        else:
            for s in stack.dependencies:
                yield('%s dependency "%s" is defined' % (name, s),
                      is_in, s, ctx.stacks)
            yield('Stack "%s" template_body is valid JSON' % name,
                  valid_stack_json, ctx, stack)

Load default var files when testing template JSON
Load default var files when testing template JSON

Since template Jinja processing has access to the template variables
we need to load the default files when testing the JSON output.

+ import os
  import json
  
  from dmaws.stacks import Stack
  from dmaws.context import Context
  
  
  def is_true(x):
      assert x
  
  
  def is_in(a, b):
      assert a in b
  
  
- def valid_stack_json(stack):
+ def valid_stack_json(ctx, stack):
-     text = stack.build('stage', 'env', {}).template_body
+     text = stack.build('stage', 'env', ctx.variables).template_body
      template = json.loads(text)
  
      assert 'Parameters' in template
      assert set(template['Parameters']) == set(stack.parameters)
      assert 'Resources' in template
  
  
+ def _load_default_vars(ctx):
+     default_vars_files = [
+         'vars/common.yml',
+     ]
+     if os.path.exists('vars/user.yml'):
+         default_vars_files.append('vars/user.yml')
+ 
+     ctx.load_variables(files=default_vars_files)
+ 
+ 
  def test_stack_definitions():
      ctx = Context()
+     _load_default_vars(ctx)
      ctx.load_stacks('stacks.yml')
  
      yield('Found stacks in the stacks.yml',
            is_true, any(isinstance(s, Stack) for s in ctx.stacks.values()))
      yield('Found groups in stacks.yml',
            is_true, any(isinstance(s, list) for s in ctx.stacks.values()))
  
      for name, stack in ctx.stacks.items():
          if isinstance(stack, list):
              for s in stack:
                  yield('Stack "%s" in group %s is defined' % (s, name),
                        is_in, s, ctx.stacks)
          else:
              for s in stack.dependencies:
                  yield('%s dependency "%s" is defined' % (name, s),
                        is_in, s, ctx.stacks)
              yield('Stack "%s" template_body is valid JSON' % name,
-                   valid_stack_json, stack)
+                   valid_stack_json, ctx, stack)
  
Load default var files when testing template JSON
## Code Before:
import json

from dmaws.stacks import Stack
from dmaws.context import Context


def is_true(x):
    assert x


def is_in(a, b):
    assert a in b


def valid_stack_json(stack):
    text = stack.build('stage', 'env', {}).template_body
    template = json.loads(text)

    assert 'Parameters' in template
    assert set(template['Parameters']) == set(stack.parameters)
    assert 'Resources' in template


def test_stack_definitions():
    ctx = Context()
    ctx.load_stacks('stacks.yml')

    yield('Found stacks in the stacks.yml',
          is_true, any(isinstance(s, Stack) for s in ctx.stacks.values()))
    yield('Found groups in stacks.yml',
          is_true, any(isinstance(s, list) for s in ctx.stacks.values()))

    for name, stack in ctx.stacks.items():
        if isinstance(stack, list):
            for s in stack:
                yield('Stack "%s" in group %s is defined' % (s, name),
                      is_in, s, ctx.stacks)
        else:
            for s in stack.dependencies:
                yield('%s dependency "%s" is defined' % (name, s),
                      is_in, s, ctx.stacks)
            yield('Stack "%s" template_body is valid JSON' % name,
                  valid_stack_json, stack)

## Instruction:
Load default var files when testing template JSON
## Code After:
import os
import json

from dmaws.stacks import Stack
from dmaws.context import Context


def is_true(x):
    assert x


def is_in(a, b):
    assert a in b


def valid_stack_json(ctx, stack):
    text = stack.build('stage', 'env', ctx.variables).template_body
    template = json.loads(text)

    assert 'Parameters' in template
    assert set(template['Parameters']) == set(stack.parameters)
    assert 'Resources' in template


def _load_default_vars(ctx):
    default_vars_files = [
        'vars/common.yml',
    ]
    if os.path.exists('vars/user.yml'):
        default_vars_files.append('vars/user.yml')

    ctx.load_variables(files=default_vars_files)


def test_stack_definitions():
    ctx = Context()
    _load_default_vars(ctx)
    ctx.load_stacks('stacks.yml')

    yield('Found stacks in the stacks.yml',
          is_true, any(isinstance(s, Stack) for s in ctx.stacks.values()))
    yield('Found groups in stacks.yml',
          is_true, any(isinstance(s, list) for s in ctx.stacks.values()))

    for name, stack in ctx.stacks.items():
        if isinstance(stack, list):
            for s in stack:
                yield('Stack "%s" in group %s is defined' % (s, name),
                      is_in, s, ctx.stacks)
        else:
            for s in stack.dependencies:
                yield('%s dependency "%s" is defined' % (name, s),
                      is_in, s, ctx.stacks)
            yield('Stack "%s" template_body is valid JSON' % name,
                  valid_stack_json, ctx, stack)
