<commit_before>#!/bin/bash

# Copyright 2018 Google, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script runs the presubmit tests, in the right order.
# It is started by prow for each PR.
# For convenience, it can also be executed manually.

set -o errexit
set -o pipefail

# Useful environment variables
readonly ELAFROS_ROOT=$(dirname ${BASH_SOURCE})/..
[[ $USER == "prow" ]] && IS_PROW=1 || IS_PROW=0
readonly IS_PROW

# Save *_OVERRIDE variables in case a cleanup is required.
readonly OG_DOCKER_REPO="${DOCKER_REPO_OVERRIDE}"
readonly OG_K8S_CLUSTER="${K8S_CLUSTER_OVERRIDE}"
readonly OG_K8S_USER="${K8S_USER_OVERRIDE}"

function restore_env() {
  export DOCKER_REPO_OVERRIDE="${OG_DOCKER_REPO}"
  export K8S_CLUSTER_OVERRIDE="${OG_K8S_CLUSTER}"
  export K8S_USER_OVERRIDE="${OG_K8S_CLUSTER}"
}

function cleanup() {
  header "Cleanup (teardown)"
  restore_env
  # --expunge is a workaround for https://github.com/elafros/elafros/issues/366
  bazel clean --expunge || true
}

function header() {
  echo "================================================="
  echo $1
  echo "================================================="
}

cd ${ELAFROS_ROOT}

# Set the required env vars to dummy values to satisfy bazel.
export DOCKER_REPO_OVERRIDE=REPO_NOT_SET
export K8S_CLUSTER_OVERRIDE=CLUSTER_NOT_SET
export K8S_USER_OVERRIDE=USER_NOT_SET

# For local runs, cleanup before and after the tests.
if (( ! IS_PROW )); then
  trap cleanup EXIT
  header "Cleanup (setup)"
  # --expunge is a workaround for https://github.com/elafros/elafros/issues/366
  bazel clean --expunge
fi

# Tests to be performed.

# Step 1: Build relevant packages to ensure nothing is broken.
header "Building phase"
bazel build //cmd/... //config/... //sample/... //pkg/... //test/...
bazel build :everything

# Step 2: Run unit tests.
header "Testing phase"
bazel test //cmd/... //pkg/...
# Run go tests as well to workaround https://github.com/elafros/elafros/issues/525
go test ./cmd/... ./pkg/...

# Step 3: Run end-to-end tests.
# Restore environment variables, let e2e-tests.sh handle them.
restore_env
./test/e2e-tests.sh
<commit_msg>Skip presubmit tests when changing only .md files (#883)<commit_after>#!/bin/bash

# Copyright 2018 Google, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script runs the presubmit tests, in the right order.
# It is started by prow for each PR.
# For convenience, it can also be executed manually.

set -o errexit
set -o pipefail

source "$(dirname $(readlink -f ${BASH_SOURCE}))/library.sh"

function cleanup() {
  "Cleaning up for teardown"
  restore_override_vars
  # --expunge is a workaround for https://github.com/elafros/elafros/issues/366
  bazel clean --expunge || true
}

cd ${ELAFROS_ROOT_DIR}

# Skip presubmit tests if only markdown files were changed.
if [[ -z "$(git status -s | grep -v '^??' | grep -v '.md$')" ]]; then
  header "Documentation only PR, skipping tests"
  exit 0
fi

# Set the required env vars to dummy values to satisfy bazel.
export DOCKER_REPO_OVERRIDE=REPO_NOT_SET
export K8S_CLUSTER_OVERRIDE=CLUSTER_NOT_SET
export K8S_USER_OVERRIDE=USER_NOT_SET

# For local runs, cleanup before and after the tests.
if (( ! IS_PROW )); then
  trap cleanup EXIT
  echo "Cleaning up for setup"
  # --expunge is a workaround for https://github.com/elafros/elafros/issues/366
  bazel clean --expunge
fi

# Tests to be performed.

# Step 1: Build relevant packages to ensure nothing is broken.
header "Verifying that everything builds"
bazel build //cmd/... //config/... //sample/... //pkg/... //test/...
bazel build :everything

# Step 2: Run unit tests.
header "Running unit tests"
bazel test //cmd/... //pkg/...
# Run go tests as well to workaround https://github.com/elafros/elafros/issues/525
go test ./cmd/... ./pkg/...

# Step 3: Run end-to-end tests.
# Restore environment variables, let e2e-tests.sh handle them.
restore_override_vars
./test/e2e-tests.sh

{'fraction_non_alphanumeric': 0.12704174228675136, 'fraction_numerical': 0.012704174228675136, 'mean_word_length': 3.9451645064805585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '627006', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1569, 'n_words': 581}