• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env ruby
2# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# Huawei Technologies Co.,Ltd.
16
17# frozen_string_literal: true
18
19require 'yaml'
20require 'logger'
21require 'optparse'
22require 'ostruct'
23
24require_relative 'generator/generator'
25
26def check_option(optparser, options, key)
27  return if options[key]
28
29  puts "Missing option: --#{key}"
30  puts optparser
31  exit false
32end
33
34def get_chunk(tests, chunk, chunks)
35  chunk_size = tests.length / chunks
36  if chunk_size > tests.length || chunk_size < 1 || chunk.negative?
37    raise "Chunk arguments are not valid: #{chunk} of #{chunks}"
38  end
39
40  result = []
41  last_idx = chunk >= chunks - 1 ? tests.length : (chunk + 1) * chunk_size
42  i = chunk * chunk_size
43  while i < last_idx
44    result.push(tests[i])
45    i += 1
46  end
47  result
48end
49
50options = OpenStruct.new
51options[:chunk] = 0
52options[:chunks] = 1
53
54optparser = OptionParser.new do |opts|
55  opts.banner = 'Usage: generate-cts.rb [options]'
56  opts.on('-t', '--template FILE', 'Path to template yaml file to generate tests (required)')
57  opts.on('-s', '--schema FILE', 'Path to json schema for template yaml (required)')
58  opts.on('-k', '--skip', 'Skip yaml schema validation')
59  opts.on('-o', '--output DIR', 'Path to directory where tests will be generated (required)')
60  opts.on('--skip-header', 'Do not generate test headers')
61  opts.on('-f', '--file FILE', 'Generate tests from the specified yaml file only, for example: fmod2.64.yaml')
62  opts.on('--chunk INTEGER', Integer, 'Chunk to process, starting from 0 (0 by default)')
63  opts.on('--chunks INTEGER', Integer, 'Number of chunks (1 by default)')
64  opts.on('-h', '--help', 'Prints this help') do
65    puts opts
66    exit
67  end
68end
69
70optparser.parse!(into: options)
71check_option(optparser, options, 'template')
72check_option(optparser, options, 'schema')
73check_option(optparser, options, 'output')
74template_path = options['template']
75schema_path = options['schema']
76output = options['output']
77skip = options['skip']
78skip_header = options['skip-header']
79chunk = options['chunk']
80chunks = options['chunks']
81file = options['file']
82
83LOG = Logger.new(STDOUT)
84LOG.level = Logger::DEBUG
85
86LOG.info "Loading '#{template_path}'"
87
88data = YAML.load_file(template_path)
89
90data['tests'] = get_chunk data['tests'], chunk, chunks
91
92data['tests'] = [{"include" => "#{file}"}] if file
93
94generator = Generator::Parser.new data, output, File.dirname(template_path), skip_header
95
96# Validate test template
97
98unless skip
99  require 'json-schema'
100  res = JSON::Validator.fully_validate(schema_path, data)
101  unless res.empty?
102    puts "Template '#{template_path}' contains several errors:"
103    puts res
104    raise 'Schema validation error, please update template to match schema to generate tests'
105  end
106end
107
108generator.parse skip
109