Class: Brut::CLI::Apps::Scaffold::CustomElementTest

Inherits:
Command
  • Object
show all
Defined in:
lib/brut/cli/apps/scaffold.rb

Instance Method Summary collapse

Methods inherited from Command

#after_bootstrap, args, #args, #before_execute, command_name, default_env, #delegate_to_commands, description, detailed_description, env_var, #err, #global_options, #handle_bootstrap_exception, #initialize, name_matches?, option_parser, #options, opts, #out, requires_project_env, requires_project_env?, #system!

Methods included from Framework::Errors

#abstract_method!, #bug!

Methods included from I18n::ForCLI

#capture, #html_escape, #safe

Methods included from I18n::BaseMethods

#l, #t, #t_direct, #this_field_value

Methods included from ExecutionResults

#abort_execution, #as_execution_result, #cli_usage_error, #continue_execution, #show_cli_usage, #stop_execution

Constructor Details

This class inherits a constructor from Brut::CLI::Command

Instance Method Details

#executeObject



553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/brut/cli/apps/scaffold.rb', line 553

def execute
  if args.empty?
    err.puts "'custom-element-test' requires one or more files to scaffold a test for"
    return 1
  end

  if args.any? { |file| Pathname(file).extname != ".js" }
    err.puts "'custom-element-test' must be given only .js files"
    return 1
  end

  files_to_create = args.map { |arg|
    path = Pathname(arg).expand_path
    relative_path = path.relative_path_from(Brut.container.js_src_dir)
    relative_path_as_spec = relative_path.dirname / (relative_path.basename(relative_path.extname).to_s + ".spec.js")
    spec_path = Brut.container.js_specs_dir / relative_path_as_spec
    [ path, spec_path ]
  }

  existing_files = files_to_create.select { |_,spec|
    spec.exist?
  }

  if existing_files.any? && !global_options.overwrite?
    relative_paths = existing_files.map { |_,pathname| pathname.relative_path_from(Brut.container.project_root) }
    err.puts "Some files to be generated already exist. Set global option --overwrite to overwrite them:"
    relative_paths.each do |file|
      err.puts file
    end
    return 1
  end

  files_to_create.each do |source_file, spec_file|
    source_class = source_file.basename(source_file.extname)
    tag_name = File.read(source_file).split(/\n/).map { |line|
      if line =~ /static\s+tagName\s*=\s*\"([^"]+)\"/
        "<#{$1}>"
      else
        nil
      end
    }.compact.first
    description = tag_name || source_class
    code =  %{import { withHTML } from "brut-js/testing/index.js"

describe("#{description}", () => {
  withHTML(`
  #{ tag_name ? "#{tag_name}" : "<!-- HTML here -->" }
  #{ tag_name ? "#{tag_name.gsub(/^</,'</')}" : "" }
  `).test("description here", ({document,window,assert}) => {
assert.fail("test goes here")
  })
})}
    if global_options.dry_run?
      out.puts "Would generate this code:\n\n#{code}"
    else
      File.open(spec_file, "w") do |file|
        file.puts code
      end
    end
  end

  0
end