<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Unit tests for terraform utils
'''

import shutil
import os
import unittest
import sys
import symphony.terraform as terraform

class TfUt(unittest.TestCase):
    '''Test Terraform operations'''
    TF_STAGING_DIR = "/tmp/symphdist"

    def setUp(self):
        print("Test setup!")
        tf_test_dir = "./testdata/tfdir"
        staging_dir = "/tmp/symphdist"
        # Set staging with sample terraform template
        shutil.rmtree(staging_dir)
        shutil.copytree(tf_test_dir, staging_dir)

        # Copy sample scripts to /tmp
        scripts = {
            "./testdata/sample_pass.sh": "/tmp/sample_pass.sh",
            "./testdata/sample_fail.sh": "/tmp/sample_fail.sh"
        }
        for script in scripts.keys():
            print("Copy %s", script)
            shutil.copy(script, scripts[script])


    def test_terraform_module_init(self):
        invalid_staging = "/tmp/nonexistent"
        tfut = terraform.Terraform(invalid_staging)
        self.assertEqual(tfut.initialized, False)

        tfut = terraform.Terraform(None)
        self.assertEqual(tfut.initialized, False)

        tfut = terraform.Terraform(TfUt.TF_STAGING_DIR)
        self.assertEqual(tfut.initialized, True)

    def test_terraform_init(self):
        ''' test terraform_init '''
        tfut = terraform.Terraform(TfUt.TF_STAGING_DIR)
        self.assertEqual(tfut.initialized, True)

        tfut.terraform_init(tf_dir="/tmp/test")

<commit_msg>shutil rmtree fails for non-existing dir. Add a check<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
Unit tests for terraform utils
'''

import shutil
import os
import unittest
import sys
import symphony.terraform as terraform

class TfUt(unittest.TestCase):
    '''Test Terraform operations'''
    TF_STAGING_DIR = "/tmp/symphdist"

    def setUp(self):
        print("Test setup!")
        tf_test_dir = "./testdata/tfdir"
        staging_dir = "/tmp/symphdist"
        # Set staging with sample terraform template
        if os.path.exists(staging_dir):
            shutil.rmtree(staging_dir)
        shutil.copytree(tf_test_dir, staging_dir)

        # Copy sample scripts to /tmp
        scripts = {
            "./testdata/sample_pass.sh": "/tmp/sample_pass.sh",
            "./testdata/sample_fail.sh": "/tmp/sample_fail.sh"
        }
        for script in scripts.keys():
            print("Copy %s", script)
            shutil.copy(script, scripts[script])


    def test_terraform_module_init(self):
        invalid_staging = "/tmp/nonexistent"
        tfut = terraform.Terraform(invalid_staging)
        self.assertEqual(tfut.initialized, False)

        tfut = terraform.Terraform(None)
        self.assertEqual(tfut.initialized, False)

        tfut = terraform.Terraform(TfUt.TF_STAGING_DIR)
        self.assertEqual(tfut.initialized, True)

    def test_terraform_init(self):
        ''' test terraform_init '''
        tfut = terraform.Terraform(TfUt.TF_STAGING_DIR)
        self.assertEqual(tfut.initialized, True)

        tfut.terraform_init(tf_dir="/tmp/test")


{'fraction_non_alphanumeric': 0.11464354527938343, 'fraction_numerical': 0.0006422607578676942, 'mean_word_length': 2.660399529964747, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '452203', 'n_tokens_mistral': 1005, 'n_tokens_neox': 1021, 'n_words': 188}