/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/**
 * Unit tests for Python 3.9
 */

plugins { id 'org.apache.beam.module' }
applyPythonNature()

// Required to setup a Python 3 virtualenv and task names.
pythonVersion = '3.9'

def posargs = project.findProperty("posargs") ?: ""

apply from: "../common.gradle"

toxTask "testPy39CloudCoverage", "py39-cloudcoverage", "${posargs}"
test.dependsOn "testPy39CloudCoverage"
project.tasks.register("preCommitPyCoverage") {
      dependsOn = ["testPy39CloudCoverage"]
}

// Dep Postcommit runs test suites that evaluate compatibility of particular
// dependencies. Each suite is exercised on at most one python version.
//
// Should still leave at least one version in PreCommit unless the marked tests
// are also exercised by existing PreCommit
// e.g. pyarrow and pandas also run on PreCommit Dataframe and Coverage
project.tasks.register("postCommitPyDep") {}

// Create a test task for supported major versions of pyarrow
// We should have a test for the lowest supported version and
// For versions that we would like to prioritize for testing,
// for example versions released in a timeframe of last 1-2 years.

toxTask "testPy39pyarrow-3", "py39-pyarrow-3", "${posargs}"
test.dependsOn "testPy39pyarrow-3"
postCommitPyDep.dependsOn "testPy39pyarrow-3"

toxTask "testPy39pyarrow-9", "py39-pyarrow-9", "${posargs}"
test.dependsOn "testPy39pyarrow-9"
postCommitPyDep.dependsOn "testPy39pyarrow-9"

toxTask "testPy39pyarrow-10", "py39-pyarrow-10", "${posargs}"
test.dependsOn "testPy39pyarrow-10"
postCommitPyDep.dependsOn "testPy39pyarrow-10"

toxTask "testPy39pyarrow-11", "py39-pyarrow-11", "${posargs}"
test.dependsOn "testPy39pyarrow-11"
postCommitPyDep.dependsOn "testPy39pyarrow-11"

toxTask "testPy39pyarrow-12", "py39-pyarrow-12", "${posargs}"
test.dependsOn "testPy39pyarrow-12"
postCommitPyDep.dependsOn "testPy39pyarrow-12"

toxTask "testPy39pyarrow-13", "py39-pyarrow-13", "${posargs}"
test.dependsOn "testPy39pyarrow-13"
postCommitPyDep.dependsOn "testPy39pyarrow-13"

toxTask "testPy39pyarrow-14", "py39-pyarrow-14", "${posargs}"
test.dependsOn "testPy39pyarrow-14"
postCommitPyDep.dependsOn "testPy39pyarrow-14"

toxTask "testPy39pyarrow-15", "py39-pyarrow-15", "${posargs}"
test.dependsOn "testPy39pyarrow-15"
postCommitPyDep.dependsOn "testPy39pyarrow-15"

toxTask "testPy39pyarrow-16", "py39-pyarrow-16", "${posargs}"
test.dependsOn "testPy39pyarrow-16"
postCommitPyDep.dependsOn "testPy39pyarrow-16"

toxTask "testPy39pyarrow-17", "py39-pyarrow-17", "${posargs}"
test.dependsOn "testPy39pyarrow-17"
postCommitPyDep.dependsOn "testPy39pyarrow-17"

toxTask "testPy39pyarrow-18", "py39-pyarrow-18", "${posargs}"
test.dependsOn "testPy39pyarrow-18"
postCommitPyDep.dependsOn "testPy39pyarrow-18"

// Create a test task for each supported minor version of pandas
toxTask "testPy39pandas-14", "py39-pandas-14", "${posargs}"
test.dependsOn "testPy39pandas-14"
postCommitPyDep.dependsOn "testPy39pandas-14"

toxTask "testPy39pandas-15", "py39-pandas-15", "${posargs}"
test.dependsOn "testPy39pandas-15"
postCommitPyDep.dependsOn "testPy39pandas-15"

toxTask "testPy39pandas-20", "py39-pandas-20", "${posargs}"
test.dependsOn "testPy39pandas-20"
postCommitPyDep.dependsOn "testPy39pandas-20"

// TODO(https://github.com/apache/beam/issues/31192): Add below suites
// after dependency compat tests suite switches to Python 3.9 or we add
// Python 2.2 support.

// toxTask "testPy39pandas-21", "py39-pandas-21", "${posargs}"
// test.dependsOn "testPy39pandas-21"
// postCommitPyDep.dependsOn "testPy39pandas-21"

// toxTask "testPy39pandas-22", "py39-pandas-22", "${posargs}"
// test.dependsOn "testPy39pandas-22"
// postCommitPyDep.dependsOn "testPy39pandas-22"

// TODO(https://github.com/apache/beam/issues/30908): Revise what are we testing

// Create a test task for each minor version of pytorch
toxTask "testPy39pytorch-19", "py39-pytorch-19", "${posargs}"
test.dependsOn "testPy39pytorch-19"
postCommitPyDep.dependsOn "testPy39pytorch-19"

toxTask "testPy39pytorch-110", "py39-pytorch-110", "${posargs}"
test.dependsOn "testPy39pytorch-110"
postCommitPyDep.dependsOn "testPy39pytorch-110"

toxTask "testPy39pytorch-111", "py39-pytorch-111", "${posargs}"
test.dependsOn "testPy39pytorch-111"
postCommitPyDep.dependsOn "testPy39pytorch-111"

toxTask "testPy39pytorch-112", "py39-pytorch-112", "${posargs}"
test.dependsOn "testPy39pytorch-112"
postCommitPyDep.dependsOn "testPy39pytorch-112"

toxTask "testPy39pytorch-113", "py39-pytorch-113", "${posargs}"
test.dependsOn "testPy39pytorch-113"
postCommitPyDep.dependsOn "testPy39pytorch-113"

// run on precommit
toxTask "testPy39pytorch-200", "py39-pytorch-200", "${posargs}"
test.dependsOn "testPy39pytorch-200"
postCommitPyDep.dependsOn "testPy39pytorch-200"

toxTask "testPy39tft-113", "py39-tft-113", "${posargs}"
test.dependsOn "testPy39tft-113"
postCommitPyDep.dependsOn "testPy39tft-113"

// TODO(https://github.com/apache/beam/issues/25796) - uncomment onnx tox task once onnx supports protobuf 4.x.x
// Create a test task for each minor version of onnx
// toxTask "testPy39onnx-113", "py39-onnx-113", "${posargs}"
// test.dependsOn "testPy39onnx-113"
// postCommitPyDep.dependsOn "testPy39onnx-113"

// Create a test task for each minor version of tensorflow
toxTask "testPy39tensorflow-212", "py39-tensorflow-212", "${posargs}"
test.dependsOn "testPy39tensorflow-212"
postCommitPyDep.dependsOn "testPy39tensorflow-212"

// Create a test task for each minor version of transformers
toxTask "testPy39transformers-428", "py39-transformers-428", "${posargs}"
test.dependsOn "testPy39transformers-428"
postCommitPyDep.dependsOn "testPy39transformers-428"

toxTask "testPy39transformers-447", "py39-transformers-447", "${posargs}"
test.dependsOn "testPy39transformers-447"
postCommitPyDep.dependsOn "testPy39transformers-447"

toxTask "testPy39transformers-448", "py39-transformers-448", "${posargs}"
test.dependsOn "testPy39transformers-448"
postCommitPyDep.dependsOn "testPy39transformers-448"

toxTask "testPy39transformers-latest", "py39-transformers-latest", "${posargs}"
test.dependsOn "testPy39transformers-latest"
postCommitPyDep.dependsOn "testPy39transformers-latest"

toxTask "testPy39embeddingsMLTransform", "py39-embeddings", "${posargs}"
test.dependsOn "testPy39embeddingsMLTransform"
postCommitPyDep.dependsOn "testPy39embeddingsMLTransform"

// Part of MLTransform embeddings test suite but requires tensorflow hub, which we need to test on
// mutliple versions so keeping this suite separate.
toxTask "testPy39TensorflowHubEmbeddings-014", "py39-TFHubEmbeddings-014", "${posargs}"
test.dependsOn "testPy39TensorflowHubEmbeddings-014"
postCommitPyDep.dependsOn "testPy39TensorflowHubEmbeddings-014"

toxTask "testPy39TensorflowHubEmbeddings-015", "py39-TFHubEmbeddings-015", "${posargs}"
test.dependsOn "testPy39TensorflowHubEmbeddings-015"
postCommitPyDep.dependsOn "testPy39TensorflowHubEmbeddings-015"

toxTask "whitespacelint", "whitespacelint", "${posargs}"

task archiveFilesToLint(type: Zip) {
  archiveFileName = "files-to-whitespacelint.zip"
  destinationDirectory = file("$buildDir/dist")

  from ("$rootProject.projectDir") {
    include "**/*.md"
    include "**/build.gradle"
    include '**/build.gradle.kts'
    exclude '**/build/**' // intermediate build directory
    exclude 'website/www/site/themes/docsy/**' // fork to google/docsy
    exclude "**/node_modules/*"
    exclude "**/.gogradle/*"
  }
}

task unpackFilesToLint(type: Copy) {
  from zipTree("$buildDir/dist/files-to-whitespacelint.zip")
  into "$buildDir/files-to-whitespacelint"
}

whitespacelint.dependsOn archiveFilesToLint, unpackFilesToLint
unpackFilesToLint.dependsOn archiveFilesToLint
archiveFilesToLint.dependsOn cleanPython

toxTask "jest", "jest", "${posargs}"

toxTask "eslint", "eslint", "${posargs}"

task copyTsSource(type: Copy) {
  from ("$rootProject.projectDir") {
    include "sdks/python/apache_beam/runners/interactive/extensions/**/*"
    exclude "sdks/python/apache_beam/runners/interactive/extensions/**/lib/*"
    exclude "sdks/python/apache_beam/runners/interactive/extensions/**/node_modules/*"
  }
  into "$buildDir/ts"
}

jest.dependsOn copyTsSource
eslint.dependsOn copyTsSource
copyTsSource.dependsOn cleanPython
